Handling has_one nested resource in Rails 3

前端 未结 2 1833
死守一世寂寞
死守一世寂寞 2021-01-20 02:22

I got a User model and a About model. The about model is a page where users have more info about them that due its nature is more appropriate to have it on a separate model

相关标签:
2条回答
  • 2021-01-20 02:48

    When using a has_one, it might make sense to declare it as a singular resource in your routes. Meaning

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

    And if you want to override the paths for new/edit, add a :path_names option to the resource/resources-call:

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

    The official documentation has lots of other tips and tricks for routing as well.

    0 讨论(0)
  • 2021-01-20 03:05

    You can use scope and controller blocks to cut down on the verbiage:

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

    which produces:

         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"}
    
    0 讨论(0)
提交回复
热议问题