2016-01-04 132 views
0

在我的應用程序可以將對象添加到我的CartItem模型,但我得到undefined method 'id' for nil:NilClass,銷燬記錄和渲染通過Ajax我的看法的時候。訪問實例變量通過Ajax請求和局部模板

所以在網頁加載時/images/7,比如我有

<div id="cart_form"> 
    <% if @cart_item_collection.include?(@image.id) %> 
    <%= render 'shared/forms/remove_from_cart', locals: { image: @image } %> 
    <% else %> 
    <%= render 'shared/forms/add_to_cart', locals: { image: @image } %> 
    <% end %> 
</div> 

class ImagesController < ApplicationController 
    def show 
    @image = Image.find(params[:id]) 
    @cart_item_collection = CartItem.where(user_id: current_or_guest_user).pluck(:image_id) 
    end 
end 

_add_to_cart.html.erb

<%= form_for(@cart_item, url: cart_item_path, :remote => true do |f| %> 
    <%= f.hidden_field :image_id, value: @image.id %> 
    <%= f.submit "Add To Cart" %> 
<% end %> 

_remove_from_cart.html.erb

<%=button_to cart_item_path(id: @cart_item), method: :delete, :remote => true, class: "icon di_darckblue extra-large button fill", name: 'images' do %> 
<i class="di-shopping-cart-up"></i> 
Remove From Cart 

正如前面提到的我可以創建一個CartItem f INE,我使這個js.erb文件

$("#cart_form").html("<%= j render partial: 'shared/forms/remove_from_cart', locals: { image: @image } %>"); 

但是,當我嘗試這就是摧毀CartItem時,我得到的錯誤

$("#cart_form").html('<%= j render partial: "shared/forms/add_to_cart", locals: { image: @image } %>'); 

我怎樣才能保持@image始終堅持每次通話?

感謝

回答

1

我們需要通過圖片ID時,使AJAX調用,以便@image對象可以在視圖中,當諧音真讓AJAX完成渲染來獲得。

因此,我們取@image使用image_id參數。

show.html.erb

<div id="cart_form"> 
    <% if @cart_item_collection.include?(@image.id) %> 
    <%= render 'shared/forms/remove_from_cart', locals: { image: @image } %> 
    <% else %> 
    <%= render 'shared/forms/add_to_cart', locals: { image: @image } %> 
    <% end %> 
    <p class="error"></p> 
</div> 

images_controller.rb

class ImagesController < ApplicationController 
    def show 
    @image = Image.find(params[:id]) 
    @cart_item_collection = CartItem.where(user_id: current_or_guest_user).pluck(:image_id) 
    end 
end 

_add_to_cart.html.erb

<%= form_for(@cart_item, url: cart_item_path, :remote => true do |f| %> 
    <%= f.hidden_field :image_id, value: @image.id %> 
    <%= f.submit "Add To Cart" %> 
<% end %> 

_remove_from_cart.html.erb

<%=button_to cart_item_path(id: @cart_item, image_id: @image.id), method: :delete, :remote => true, class: "icon di_darckblue extra-large button fill", name: 'images' do %> 
<i class="di-shopping-cart-up"></i> 
Remove From Cart 

添加到購物車

def add_to_cart 
    @image = Image.find(params[:cart_item][:image_id]) 
    #if @image is present 
     #your code 
    #else 
     #@error= "Item could not be added to cart." 
    #end 
end 

create.js.erb

<% if [email protected] %> 
    $("#cart_form").html("<%= j render partial: 'shared/forms/remove_from_cart', locals: { image: @image } %>"); 
    $('.error').html(''); 
<% else %> 
    $('.error').html(<%= @error %>); 
<% end %> 

從購物車中取出,

def destroy 
    @image = Image.find(params[:image_id]) 
    #if @image 
     #destroying @cart_item 
     #rendering destroy.js.erb 
     #@cart_item = CartItem.new 
    #else 
     @error="Item could not be removed from cart" 
    #end 
end 

destroy.js。ERB:

<% if [email protected] %> 
    $("#cart_form").html('<%= j render partial: "shared/forms/add_to_cart", locals: { image: @image } %>'); 
    $('.error').html(''); 
<% else %> 
    $('.error').html(<%= @error %>); 
<% end %> 

另一種方式: 當你只是用諧音裏面圖片ID: 在這裏我們只是路過的image_id在控制器和發送回前端。

但是我們唯一需要做的檢查就是檢查傳遞給控制器​​的image_id是否存在圖像,因爲用戶可以在前端更改該值。

檢查是否需要在每次AJAX調用時檢查圖像是否存在,這使得第一種方法更安全。但是,如果這對你無關緊要,那麼你可以採用這種方法。

<div id="cart_form"> 
    <% if @cart_item_collection.include?(@image.id) %> 
    <%= render 'shared/forms/remove_from_cart', locals: { image_id: @image.id } %> 
    <% else %> 
    <%= render 'shared/forms/add_to_cart', locals: { image_id: @image.id } %> 
    <% end %> 
</div> 

class ImagesController < ApplicationController 
    def show 
    @image = Image.find(params[:id]) 
    @cart_item_collection = CartItem.where(user_id: current_or_guest_user).pluck(:image_id) 
    end 
end 

_add_to_cart.html.erb

<%= form_for(@cart_item, url: cart_item_path, :remote => true do |f| %> 
    <%= f.hidden_field :image_id, value: image_id %> 
    <%= f.submit "Add To Cart" %> 
<% end %> 

_remove_from_cart.html.erb

<%=button_to cart_item_path(id: @cart_item, image_id: image_id), method: :delete, :remote => true, class: "icon di_darckblue extra-large button fill", name: 'images' do %> 
<i class="di-shopping-cart-up"></i> 
Remove From Cart 

添加到購物車,我使這個js.erb文件,

def add_to_cart 
    @image_id = params[:cart_item][:image_id] 
    #your code 

end 

create.js.erb

$("#cart_form").html("<%= j render partial: 'shared/forms/remove_from_cart', locals: { image_id: @image_id } %>"); 

從購物車中取出,

def destroy 
    @image = params[:image_id] 
    #destroying @cart_item 
    #rendering destroy.js.erb 
end 

destroy.js.erb:

$("#cart_form").html('<%= j render partial: "shared/forms/add_to_cart", locals: { image_id: @image_id } %>'); 
+0

心不是事實,即時得到'未定義@image上的nil:NilClass'的方法'id'是否意味着@image inst可用或不存在? – Richlewis

+0

並且這個檢查是否在_add_to_cart.html.erb? – Richlewis

+0

因此,如果一個項目從購物車中刪除,圖像會發生什麼?你是從'def remove_from_cart'方法傳遞它嗎? – sahil