两次嵌套表单路径问题

时间:2014-09-16 14:49:39

标签: ruby-on-rails routes nested-attributes

我的user_calendar模型有以下表格:

<%= form_for([company,user,calendar], :remote => true) do |f| %>
    ....
<% end %>

路线如下:

scope '(:locale)' do
  resources :companies do
    resources :users do
      resources :user_calendar
    end
  end
end

当用户点击用户/主页上的修改时:

<%= link_to 'E', edit_company_user_path(user.company, user), :remote => true %>

从users_controller调用该方法:

def edit
  @calendar = @user.user_calendars.build
  respond_to do |format|
    format.js
  end
end

edit.js应该加载表单的模型数据:

$('#calendar-form').html("<%= escape_javascript(render :partial => 'user_calendars/form', :locals => { :company => @company, :user => @user, :calendar => @calendar }) %>");

但是我没有渲染表单而是出现以下错误:

ActionView::Template::Error (undefined method `company_user_user_calendars_path' for #<#<Class:0x007fc351d12808>:0x00000001cf6128>):

三个@company,@ user和@calendar具有正确的数据,但路径仍显示错误。这是user_calendars控制器上rake路由的结果:

Controller#Action
company_user_user_calendar_index GET    (/:locale)/companies/:company_id/users/:user_id/user_calendar(.:format)          user_calendar#index
                             POST   (/:locale)/companies/:company_id/users/:user_id/user_calendar(.:format)          user_calendar#create
  new_company_user_user_calendar GET    (/:locale)/companies/:company_id/users/:user_id/user_calendar/new(.:format)      user_calendar#new
 edit_company_user_user_calendar GET    (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id/edit(.:format) user_calendar#edit
  company_user_user_calendar GET    (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format)      user_calendar#show
                             PATCH  (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format)      user_calendar#update
                             PUT    (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format)      user_calendar#update
                             DELETE (/:locale)/companies/:company_id/users/:user_id/user_calendar/:id(.:format)      user_calendar#destroy

1 个答案:

答案 0 :(得分:0)

可能因为:resources :user_calendar

而发生

如果您需要resources,则应撰写resources :user_calendars - (s

你也可以关注resources/resource