js.erb文件中simple_form_for标记中缺少块错误

Missing Block error in simple_form_for tag in js.erb file

本文关键字:错误 form erb 文件 simple js for      更新时间:2023-09-26

我正在尝试显示一个注释列表和一个使用AJAX生成新注释的表单。simple_form_for块给出以下错误:DeliveryNegotiations#show中的ArgumentError显示/home/action/socialpost/app/views/delivery_expressions/show.js.erb,其中第10行出现:丢失块

这是show.js.erb文件:

$("#conversation").html(
    '<% @comments.each do |com| %> '
        <strong> <%=j "User " + com.author_id.to_s + ":" %> </strong> '
        <%=j (com.comment || " ") %> '
        <br/> '
    <% end %> '
  <fieldset> '
    <legend> '
      New comment '
    </legend> '
    <%=j form_for ([@delivery_request, @delivery_negotiation, @comment]) do |builder| %> '
      <%=j builder.text_area :comment %> '
      <%=j builder.hidden_field :author_id, value: current_user.id %> '
      <%=j builder.submit %> '
    <% end %> '
  </fieldset>'
');

如有任何帮助,我们将不胜感激。

请将代码放入一个分部中,并从js.erb文件中呈现,因为更改很容易。

在_comments_form.html.erb 中

  <% @comments.each do |com| %>
      <strong> <%= "User " + com.author_id.to_s + ":" %> </strong> 
      <%= (com.comment || " ") %>
      <br/>
  <% end %>

  <fieldset> 
    <legend> 
      New comment
    </legend> 
    <%= form_for ([@delivery_request, @delivery_negotiation, @comment]) do |builder| %> 
      <%= builder.text_area :comment %> 
      <%= builder.hidden_field :author_id, value: current_user.id %> 
      <%= builder.submit %>
    <% end %> 
  </fieldset>

在js.erb文件中

$("#conversation").html("<%=escape_javascript(render 'comments_form')%>");

我希望这对你有帮助。