2016-03-27 36 views
0

初學者在這裏,所以請耐心等待。 :)index.html.erb中的部分渲染將不會顯示在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 

「還是同樣的錯誤。

任何幫助將不勝感激。

謝謝!

回答

1

要渲染的部分從index.html.erb文件,如:

<%= render product %> 

這裏,product變量沒有被定義爲在視圖文件中的局部變量。從您的控制器的索引操作傳遞產品變量作爲實例變量,如@product或在index.html頁面中定義它。

或者,你可以簡單地做這樣的:在產品 在products_controller>指數

def index 
    @products = Product.all 
end 

> index.html.erb

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

這個跑步,我在'products_controller'去了,把這個: '高清指數 @product = Product.find(PARAMS [:編號]) end' ,仍然得到同樣的錯誤。 :( –

+0

如果你使用傳統的rails路由,那麼'products_controller> index'不接受任何** id **參數。因此,如果你想這樣做,那麼你需要自定義索引方法route。 – Emu

+0

好的,修改'methods> product.rb''的規則爲'index'。'稍後再試。謝謝!:) –

1

我猜你可能有一些循環如下在你的index.html.erb

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

這裏product中的render調用指的是你在迭代器的塊中定義的局部變量。

但是當您嘗試將相同的代碼複製/粘貼到您的show.html.erb時,product將被視爲未定義,因爲它不在循環中。

所以,你應該使用下列內容:

<%= render @product %> 

show.html.erb,假設你必須在你的show行動已分配@product

+0

夥計,這正是我的'index.html.erb'看起來像!和'<%= render @product%>'在我的'show.html.erb'中工作!謝謝!你們真棒! –

0

@Emu和@Dharam建議我在產品show.html.erb

而不是<%= render product %>

,並確保@product在我products_controller.rb定義的,我做到了:

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

它的工作!沒有更多undefined錯誤。

雖然我確實有一些樣式問題。長話短說,我複製並粘貼了我的_product_row部分內容(請參見上面問題中的原始代碼),而不是將其呈現在我的產品show中。然後我根據Emu和Dharam的建議修改了一下。基本上它種植的想法,嘗試添加@ 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

我希望這是有道理的。再次感謝所有人。 :)