将 form_for 与嵌套资源一起使用 [英] Using form_for with Nested Resources

查看:21
本文介绍了将 form_for 与嵌套资源一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个待办事项应用程序,以尝试熟练使用 Rails.我的应用程序中有四个层次结构.

I am building a To Do application in an attempt to get fluent with Rails. There are four levels of hierarchy in my app.

  1. 用户
  2. 目标(每个用户有多个目标)
  3. 任务(每个目标有多个任务)
  4. 子任务(每个任务有多个子任务)

对于每个使用嵌套资源的前端表单,我都有一个可用的前端表单.我的 routes.rb 有这样的东西

I have a working front end form for each of these that utilizes nested resources. My routes.rb has something like this

 resources :goal do
  resources :task do
    resources :subtask
  end
 end

我现在想做的是在用户控制器的一个视图中拥有所有这些表单.

What I would like to do now is that have all these forms right in one of the views of the user controller.

这是我尝试创建的表单:

This the form that I have attempted to create :

<%= form_for @task, url: {controller: 'task', action: 'create'} do |f| %>

 <%= f.label :description %>

 <%= f.text_field :description %>

 <%= f.submit "Add Goal", class: "btn" %>

<% end %>

但我最终收到此错误

No route matches {:action=>"create", :controller=>"task", :id=>"1"}

:id=>1 对应我所在的用户页面 (http://localhost:3000/user/1)

The :id=>1 corresponds to the user page I am on (http://localhost:3000/user/1)

我的理解是,我没有在任何地方提供此步骤所针对的目标 ID.不知道如何实现这一点.

What I understand is that there is that nowhere have I provided the goal_id for which this step is intended. No idea how to implement this.

我注意到的另一件事是对 rake routes 的响应显示了很多 URI 路径,但没有显示 POST 方法.它不允许我在 form_for 中的 url: 中使用路径,因为它与 POST 方法不匹配.

Another thing that I have noticed is that a response to rake routes shows a lot of URI paths but nothing for POST method. It does not allow me to use a path from there in the url: in form_for because it does not match the POST method.

所以我的问题是:

  1. 当你有嵌套资源时如何路由一个 form_for ?
  2. 如何在使用 form_for 时提供父资源的 ID 以便正确路由我的创建操作?

推荐答案

嵌套路由超过两层深度通常是不好的做法.我会将您的路线更改为:

It is typically bad practice to nest routes beyond two levels deep. I would change your routes to:

  resources :goal do
    resources :task 
  end

  resources :task do
      resources :subtask
  end

现在,如果您在命令行中运行bundle exec rake routes",您将看到所有嵌套的路由及其相应的帮助程序.您当前的问题在于 form_for 方法.您需要添加其嵌套的资源,在这种情况下应该是:

Now if you run "bundle exec rake routes" in the command line you will see all of the nested routes and their corresponding helpers. Your current issue lies with the form_for method. You need to add the resource its nested with which in this case should be:

<%= form_for [@goal,@task] do |f| %>
    blah blah
<% end %>

最后,@goal 也仍未定义,因此您需要在任务控制器的新"操作中定义它.这通常是通过传递您的任务将通过 params 散列和用于获取新"表单的link_to"关联的目标的 id 来完成的.然后在您的任务控制器中的新操作中:

Lastly, @goal is also still undefined so you'll need to define it in your 'new' action in the tasks controller. This is normally done by passing the id of the goal your task will be associated with via the params hash and the "link_to" used to get to the 'new' form. Then in the new action in your tasks controller:

@goal = Goal.find(params[:goal_id]) #The parameter can be named anything
@task = Task.new

然后在您的创建"操作中,您应该建立关联:

Then in your 'create' action you should have the association made:

@task = Goal.tasks.new(task_params)
if @task.save
   flash[:success] = "Woot" 
   redirect_to somewhere_awesome_path
else
  whatever
end

这篇关于将 form_for 与嵌套资源一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆