我正在尝试为具有双重嵌套路由simple_form_for
的 Rails 3.2构建一个,例如:@objects
/users/1/projects/2/objects
表格是:
<%= simple_form_for @object, :url => user_project_objects_path(@user, @project), :html => { :class => 'form-horizontal' } do |f| %>
在 routes.rb 中:
resources :users do
resources :projects do
resources :objects do
collection { post :import }
end
end
end
我的问题是:控制器new
的create
作用是什么?Object
到目前为止 - 我收到路由错误 - 我有:
def create
@user = current_user
@project = Project.find_by_user_id(@user)
@object = @project.objects.build(params[:object])
if @object.save
flash[:notice] = "Object was successfully created"
redirect_to user_project_objects_path
else
render 'new'
end
end
def new
@user = current_user
@project = Project.find_by_user_id(@user)
@object = @project.objects.build
end
非常感谢任何建议。
回应格雷格 W:
No route matches {:action=>"edit", :controller=>"objects", :user_id=>#<Object id: 8, source_lang_id: 1, source_content: "Kaffehaus", target_lang_id: 2, target_content: "cafe", domain_id: 2, owner_id: nil, created_at: "2013-01-04 06:36:55", updated_at: "2013-01-04 06:36:55", project_id: 2>}
(owner_id
用户,即 current_user)未更新 - 这可能是问题(?)