Handling a has_one of a nested resource in Rails 3

I have a User model and an About model. An example model is a page on which users have more information about them, since its nature is more appropriate for its use on a separate model, and not in the user model.

I want to be able to direct it to something like /: username / about and get all the verbs working in this way (GET POST, PUT, DELETE).

/:username/about
/:username/about/edit
/:username/about

This is what I already have

# routes.rb
resources :users do 
  resources :abouts
end

match ':username/about' => 'abouts#show', :as => :user_about
match ':username/about/add' => 'abouts#new', :as => :user_new_about    
match ':username/about/edit' => 'abouts#edit', :as => :user_edit_about

And in the models I

# about.rb
belongs_to :user

# user.rb
has_one :about

When I make a message or put in / roses / about It interprets it as a show

Started POST "/roses/about" for 127.0.0.1 at Sun Feb 27 16:24:18 -0200 2011
  Processing by AboutsController#show as HTML

I probably miss the declaration in the routes, but isn’t it confused by declaring every verb for the resource when it differs from the standard?

What is the easiest and cleanest way to archive this?

+3
2

scope controller :

  scope "/:username" do
    controller :abouts do
      get 'about' => :show
      post 'about' => :create
      get 'about/add' => :new
      get 'about/edit' => :edit
    end
  end

:

     about GET /:username/about(.:format) {:action=>"show", :controller=>"abouts"}
           POST /:username/about(.:format) {:action=>"create", :controller=>"abouts"}
 about_add GET /:username/about/add(.:format) {:controller=>"abouts", :action=>"new"}
about_edit GET /:username/about/edit(.:format) {:controller=>"abouts", :action=>"edit"}
+4

has_one .

resources :users do
  resource :about # notice "resource" and not "resources"
end

/, :path_names /-:

resources :about, :path_names => { :new => 'add', :edit => 'edit' }

.

+11

Source: https://habr.com/ru/post/1795093/


All Articles