开发者

routing urls and paths in rails

开发者 https://www.devze.com 2022-12-10 07:44 出处:网络
I have a controller which has a method called history class UsersController < ApplicationControll开发者_如何学Cer

I have a controller which has a method called history

class UsersController < ApplicationControll开发者_如何学Cer

  def history
    User.return_history(params[:id])
  end

end

I have the following in my routes.rb file

map.resources :users, :shallow => true do |user|
    user.resources :friends, :shallow => false
    user.resources :posts, :collection=>{:no_access => :get}
    user.resources :photos
end

How do I try to Ajax call the history method of the users_controller.rb? Using link_to_remote in the following way

link_to_remote 'History', :url=>history_user_path(@user), :update=>"history", :method=>'get'

throws me an error saying history_user_path() not found. How can this be? edit_user_path() shows no error and edit is not even explicitly defined in the User.rb file. Thanks.


mapresources :users creates a bunch of url/path helper methods, including edit_users_path. If you need others. you've got to add it as either a :member, or :collection option for map.resources.

This will let you do what you want:

map.resources :users, :shallow => true, :member => {:history => :get} do |user|
    user.resources :friends, :shallow => false
    user.resources :posts, :collection=>{:no_access => :get}
    user.resources :photos
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号