Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails——传递两个不同变量时两次呈现部分时出错_Ruby On Rails - Fatal编程技术网

Ruby on rails RubyonRails——传递两个不同变量时两次呈现部分时出错

Ruby on rails RubyonRails——传递两个不同变量时两次呈现部分时出错,ruby-on-rails,Ruby On Rails,我正在尝试对页面中的多个部分使用部分表单。第一次,我通过了“部分:”o“,第二次,我通过了“部分:”s“ 当我传递第一个变量时,它成功地工作,但在第二次呈现form partial时,它生成了一个“未定义的方法`humanize'for nil:NilClass”错误 下面是呈现附加部分的表单示例: <some form stuff here> <%= render partial: "item_form", locals: {f: f, section: "o"} %>

我正在尝试对页面中的多个部分使用部分表单。第一次,我通过了“部分:”o“,第二次,我通过了“部分:”s“

当我传递第一个变量时,它成功地工作,但在第二次呈现form partial时,它生成了一个“未定义的方法`humanize'for nil:NilClass”错误

下面是呈现附加部分的表单示例:

<some form stuff here>
<%= render partial: "item_form", locals: {f: f, section: "o"} %>
<some form stuff here>
<%= render partial: "item_form", locals: {f: f, section: "s"} %>

下面是正在呈现的_item_form.html.erb部分的一部分:

  <% unless @item.paragraphs.empty? %>
    <% @item.paragraphs.each do |paragraph| %>
      <% field_type = "Paragraph" if paragraph.paragraph_type == "#{section}paragraph" %>
      <% field_type = "Figure description" if paragraph.paragraph_type == "#{section}figure" %>
      <% field_type = "Bullet" if paragraph.paragraph_type == "#{section}bullet" %>
        <div class="form-group">
          <%= f.label field_type, class: "col-sm-2 control-label" %>

我得到的错误突出显示了f.label行(上面示例中的最后一行),并抛出错误“未定义的方法`humanize'for nil:NilClass”


知道这是怎么回事吗?如果删除第一个渲染,它仍然无法工作。如果我删除第一个渲染,并将第二个部分更改为“o”而不是“s”,则它可以工作。在节中添加除“o”以外的任何其他字母都会将其拧紧。

字段类型仅在某些情况下定义,如代码中所示:

<% field_type = "Paragraph" if paragraph.paragraph_type == "#{section}paragraph" %>
<% field_type = "Figure description" if paragraph.paragraph_type == "#{section}figure" %>
<% field_type = "Bullet" if paragraph.paragraph_type == "#{section}bullet" %>

你确定其中至少有一个是真的吗

我的猜测是,你确实有一个带有oparagraph类型的段落,比如figure或obullet,但没有sparagraph、sfigure或sbullet类型,所以最后你会有一个nil field_类型

我不知道应用程序背后的业务逻辑,但我建议定义一个标准案例(当然,如果可以的话),然后在必要时进行更新,如:

<% field_type = "Paragraph" %>
<% field_type = "Figure description" if paragraph.paragraph_type == "#{section}figure" %>
<% field_type = "Bullet" if paragraph.paragraph_type == "#{section}bullet" %>


我还建议将此逻辑移到帮助器,而不是在视图本身中决定字段标签名称。

Ahh ok。我没有意识到,出于某种原因,它在抱怨field_类型。我以为它在抱怨f。谢谢你的建议。我现在要走那条路。我想我可以在你的帮助下解决这个问题。再次感谢