繁体   English   中英

在index.html.erb中呈现的部分呈现在show.html.erb中不会显示

[英]Partial Rendered in index.html.erb Won't Display in show.html.erb

这里的初学者,所以,请忍受我。 :)

我有一个局部变量,该局部变量可以在我的views > products > index.html.erb正确显示:

<div>
 <table>
  ...
   <tbody>
    <%= render product %>
   </tbody>
  ...
 </table>
</div>

这是_product部分:

<div>
 <td>
  <%= render "product_row", product: product, order_item: @order_item %>
 </td>
</div>

..指向此_product_row部分:

<div>
  <%= form_for order_item, remote: true do |f| %>
   <%= number_to_currency product.price %>
   <%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
   <%= f.hidden_field :product_id, value: product.id %>
   <%= f.submit %>
  <% end %>
</div>

一切都很好,但是我想在我的views > products > show.html.erb显示_product_row 因此,我将其复制并粘贴并得到此错误:

NameError in Products#show
undefined local variable or method `product' for #<#<Class:...>
Extracted source:
<%= render "product_row", product: product, order_item: @order_item %>

...所以我进入我的products_controller并输入以下内容:

def show
 @product = Product.find(params[:id])
  respond_to do |format|
   format.html # show.html.erb
   format.json { render json: @product }
  end
end

'仍然是同样的错误。

任何帮助将不胜感激。

谢谢!

您正在从index.html.erb文件渲染部分内容,例如:

<%= render product %>

此处, product变量在视图文件中未定义为局部变量。 从控制器的index动作中,将product变量作为实例变量传递给@product或在index.html页中对其进行定义。

或者,您可以像这样简单地做:在products_controller> index

def index 
  @products = Product.all
end

产品中> index.html.erb

<% @products.each do |product| %>
<div>
 <table>
  ...
   <tbody>
    <%= render product %>
   </tbody>
  ...
 </table>
</div>
<% end %>

我想您的index.html.erb可能存在如下循环

<% @posts.each do |post| %>
  <%= render product %>
<% end %>

这里的render调用中的product指的是您在迭代器块中定义的局部变量。

但是,当您尝试将相同的代码复制/粘贴到show.html.erbproduct将被视为未定义,因为它是循环的。

因此,您应该使用以下内容:

<%= render @product %>

show.html.erb ,假设您必须在show操作中分配@product

@Emu和@Dharam建议我放

<%= render @product %>而不是<%= render product %>

在我的产品show.html.erb ,并确保在我的products_controller.rb定义了@product ,我这样做:

def show @product = Product.find(params[:id]) end

有效! 没有更多undefined错误。

我确实有一些样式问题。 长话短说,我将_product_row部分中的内容(请参阅上面问题中的原始代码)复制并粘贴到了我的产品show而不是渲染它。 然后我根据E和达兰的建议做了一些修改。 基本上,它植入了尝试在错误未定义的地方添加@ s的想法:

<div>
 <%= form_for @order_item, remote: true do |f| %>
  <h4 style="font-family: 'Share Tech Mono', sans-serif;">The Small Price to Pay: 
   <span style="color: green">
    <%= number_to_currency @product.price %>
   </span>
  </h4>
  <h4 style="font-family: 'Share Tech Mono', sans-serif;">How many:</h4>
  <div class="input-group">
   <%= f.number_field :quantity, value: 1, class: "form-control", min: 1 %>
   <div class="input-group-btn, add-to-cart-button">
    <%= f.hidden_field :product_id, value: @product.id %>
    <%= f.submit "ADD IT", class: "btn btn-danger" %>
   </div>
  </div>
 <% end %>
</div>

然后将其添加到我的products_controller.rb

def show @product = Product.find(params[:id]) @order_item = current_order.order_items.new end

我希望这是有道理的。 再次谢谢大家。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM