【问题标题】:Rails nested Resources not working in controllerRails嵌套资源在控制器中不起作用
【发布时间】:2015-03-01 03:44:33
【问题描述】:

我有这个嵌套资源:

resources :services do
  resources :users  do
    put "assign" => "services#users#assign", as: :assign
  end 
end

我的表单包含这个:

<%= button_to 'submit', service_user_assign_url(service.id, abstractor.id), method: :put %>

这会生成以下网址,我觉得很好:

http://localhost:3000/services/1/users/2/assign

以下内容在我的服务控制器中:

def assign
  @service = Service.find(params[:service_id])
  @service.users << User.find(params[:user_id])
  redirect_to dashboards_path
end

但是我得到了这个错误:

 The action 'users' could not be found for ServicesController

我不确定这意味着什么 - 我在用户和服务之间有很多关系,我正在尝试将现有用户与服务相关联

【问题讨论】:

  • 这可能是由于“services#users#assign”。我从来没有见过。您指的是“services#assign”吗?

标签: ruby-on-rails associations nested-resources


【解决方案1】:

你需要告诉rails这个action是member action还是collection action,从你提到的你要使用的url中,它是一个member action:

resources :services do
  resources :users  do
    member do
      put :assign
    end
  end 
end

【讨论】:

  • 基本正确,除了是会员而不是会员
猜你喜欢
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多