【问题标题】:Deeply Nested resources Form深度嵌套的资源表单
【发布时间】:2014-05-30 16:00:36
【问题描述】:

我正在努力让这个工作,我已经阅读了很多,但找不到这里的问题。

routes.rb

  resources :scripts do
    resources :reviews
    resources :issues do
      resources :comments
    end
  end

cmets_migration

create_table :comments do |t|
  t.integer :issue_id
  t.integer :user_id
  t.text :body

  t.timestamps
end

控制器动作

  def create
    @issue = Issue.find(params[:issue_id])
    @comment = current_user.comments.build(comment_params)
    @comment.issue_id = @issue.id

    if @comment.save
      redirect_to @comment, notice: 'Comment was successfully created.'
    else
      render :new
    end
  end

  def new
    @issue = Issue.find(params[:issue_id])
    @comment = current_user.comments.new
    @comment.issue_id = @issue.id
  end

现在在我的Issues/Show 视图中,我想添加用于添加评论的表单:

<%= form_for [@issue, @comment] do |f| %>
  <div class="field">
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

路线:

                          POST   /scripts/:script_id/issues/:issue_id/comments(.:format)          comments#create
 new_script_issue_comment GET    /scripts/:script_id/issues/:issue_id/comments/new(.:format)      comments#new
edit_script_issue_comment GET    /scripts/:script_id/issues/:issue_id/comments/:id/edit(.:format) comments#edit
     script_issue_comment GET    /scripts/:script_id/issues/:issue_id/comments/:id(.:format)      comments#show
                          PATCH  /scripts/:script_id/issues/:issue_id/comments/:id(.:format)      comments#update
                          PUT    /scripts/:script_id/issues/:issue_id/comments/:id(.:format)      comments#update
                          DELETE /scripts/:script_id/issues/:issue_id/comments/:id(.:format)      comments#destroy
            script_issues GET    /scripts/:script_id/issues(.:format)                             issues#index

这给了我First argument in form cannot contain nil or be empty

虽然请求信息显示:

{"action"=>"show", "controller"=>"issues", "script_id"=>"10", "id"=>"8"}

我是否必须在 cmets 中也包含 :script_id

我在这里错过了什么?

【问题讨论】:

  • 如果您在 show 操作中遇到错误,查看它的控制器代码会很有帮助。
  • 我正在尝试在Issues Show View 中调用Comments 表单。您的意思是来自Issues 的显示操作?
  • 是的,来自问题控制器的 show 操作
  • 表演动作里面没有东西。我也需要在那里声明吗? (如果这是愚蠢的,请原谅我,我还在学习)

标签: ruby-on-rails ruby ruby-on-rails-4 form-for nested-resources


【解决方案1】:

您可以使用简单的form_for 来创建评论:

form_for @issue.comments.build, url: script_issue_comments_path(params[:script_id], @issue) do |f|
  f.text_area :body
  f.submit "save"
end

【讨论】:

  • 干杯垫,这给了我undefined method issue_path' for #:0x007fe4f20c2888>`,我错过了什么吗?
  • shallow 让事情变得更简单还是更混乱?我应该选择浅:真的吗?
  • 尝试使用这个:form_for @issue, url: script_issue_path(@issue.script, @issue) 但我认为你不应该这样做,我认为 Rails 应该自己猜测这条路径......
  • 相同的输出:/我还必须包含 script_id 吗?脚本 > 问题 > 评论
  • 错误是否来自与form_for 相同的行?是的,因为它是 2 个嵌套资源,您需要 2 个资源的 ID
【解决方案2】:

您错过了基于new 操作而不是create 的表单。您还需要在此处声明这些变量。

【讨论】:

  • 我也必须包含 :script_id 吗?我也在New Action 中声明了变量。输出相同。
  • 如果这个 from 显示在 show action 上,你需要在那里设置。
【解决方案3】:

这是因为 form_for 中的参数为零。你应该在你的表演动作中初始化它。你不需要 script_id

class IssuesController < ApplicationController
    def show
     ..
     @comment = @issue.comments.build
     ..
end

修复未定义路径错误。您将需要稍微修改 form_for。

<% @form_for @comment, url: script_issue_comments_path(@issue.script_id, @issue) do |f| %>
...
<% end %>

【讨论】:

  • 我在New Action 中初始化了它。还是一样
  • 我没有注意到您正在尝试在问题显示页面中执行此操作,我已编辑我的答案,请尝试是否有帮助
  • #:0x007fe4ed145198> 的未定义方法 `issue_cmets_path' 仍然:/
  • 好的,这是因为您的问题嵌套在脚本中。因此,在这种情况下,您可能需要在 url 中使用 script_id。有一个解决方法,我将编辑我的回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 2011-09-26
  • 1970-01-01
相关资源
最近更新 更多