gpt4 book ai didi

ruby-on-rails - rails 嵌套路由的简单形式

转载 作者:行者123 更新时间:2023-12-01 01:12:56 24 4
gpt4 key购买 nike

我正在尝试构建一个 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的 Action Controller ?

到目前为止 - 我遇到了路由错误 - 我有:
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

非常感谢任何建议。

回复 Greg 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)未更新 - 这可能是问题(?)

最佳答案

在表格中,您可以使用

simple_form_for([@user, @project, object]) do

您可以使用关系来获取数据,如
@project= current_user.projects

代替
@project = Project.find_by_user_id(@user)

关于ruby-on-rails - rails 嵌套路由的简单形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14151166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com