所以,我有以下链接:
<%= link_to(outing_add_guests_path, :class => 'modal') do %>
<div id="notImportant"></div>
<% end %>
当我点击它时,Rails告诉我
No route matches {:controller=>"outings", :action=>"add_guests"}
但是,这是我的路线文件:
resources :outings do
get "/add_guests" => "outings#add_guests"
post "/add_guests" => "outings#add_guests"
delete "/remove_guests" => "outings#remove_guests"
end
以及我的Outings Controller的相应操作:
def add_guests
@outing_guest = OutingGuest.new(:outing_id => params[:outing_id])
@outing_guest.user_id = params[:user_id]
if @outing_guest.save
flash[:notice] = "Guest added successfully"
redirect_to({ :action => 'outing', :id => params[:outing_id] })
else
flash[:notice] = "Guest could not be added"
redirect_to({ :action => 'outing', :id => params[:outing_id] })
end
end
有没有理由Rails无法检测到我的控制器或其动作?
编辑:以下是佣金路线的部分结果
outing_add_guests GET /outings/:outing_id/add_guests(.:format) outings#add_guests
POST /outings/:outing_id/add_guests(.:format) outings#add_guests
答案 0 :(得分:1)
我注意到你的link_to
与其他路线不一致
outing_add_guests_path
outings_add_guests_path
您是否rake routes
验证outing_add_guests_path
是否存在?
编辑:
您的rake routes
显示您需要outing_id
,因此您的路线设置不正确(至少不适用于POST)。我会按照@RyanBigg建议的方式修复它们。
答案 1 :(得分:1)
您应该使用collection
方法定义这些路线:
resources :outings do
collection do
get :add_guests
post :add_guests
delete :remove_guests
end
end
这样做是为指定的操作定义新路由,以及自动定义这些路由的路由助手。有关详细信息,请阅读Routing Guide。