在rails中路由URL和路径

时间:2009-10-30 05:18:49

标签: ruby-on-rails ajax routes

我有一个控制器,它有一个名为history

的方法
class UsersController < ApplicationController

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

end

我的routes.rb文件中有以下内容

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

如何尝试Ajax调用users_controller.rb的历史记录方法?以下列方式使用link_to_remote

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

错误地说我找不到history_user_path()。怎么会这样? edit_user_path()显示没有错误,甚至在User.rb文件中也没有明确定义编辑。感谢。

1 个答案:

答案 0 :(得分:2)

mapresources:users创建了一堆url / path helper方法,包括edit_users_path。如果你需要别人。你必须将它添加为:member或者:map.resources的集合选项。

这可以让你做你想做的事:

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