Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't mass-assign protected attributes: item #24

Open
pailoro opened this issue Aug 14, 2014 · 10 comments
Open

Can't mass-assign protected attributes: item #24

pailoro opened this issue Aug 14, 2014 · 10 comments

Comments

@pailoro
Copy link

pailoro commented Aug 14, 2014

I'm using this plugin with the convetions, but get this error:

ERROR:

Can't mass-assign protected attributes: item

class ShoppingCartsController < ApplicationController
before_filter :extract_shopping_cart
def create
@Product = Video.find(params[:product_id])
@shopping_cart.add(@Product, @product.price)
redirect_to shopping_cart_path
end
def show

@muhammadn
Copy link

Could you show what your models for shopping_cart and shopping_cart_item ?

@pailoro
Copy link
Author

pailoro commented Aug 18, 2014

Controller:

class ShoppingCartsController < ApplicationController
  before_filter :extract_shopping_cart
  def create
    @product = Video.find(params[:product_id])
    @shopping_cart.add(@product, @product.price) # the error is generated on that line
    redirect_to shopping_cart_path
  end

Heres my model ShoppingCart

# == Schema Information
#
# Table name: shopping_carts
#
#  id         :integer          not null, primary key
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class ShoppingCart < ActiveRecord::Base
    acts_as_shopping_cart

    attr_accessible :price

    #accepts_nested_attributes_for :price
end

and the model ShoppingCartItem

# == Schema Information
#
# Table name: shopping_cart_items
#
#  id         :integer          not null, primary key
#  owner_id   :integer
#  owner_type :string(255)
#  quantity   :integer
#  item_id    :integer
#  item_type  :string(255)
#  price      :float
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class ShoppingCartItem < ActiveRecord::Base
  attr_accessible :owner_id, :owner_type, :quantity, :item_id, :item_type, :price
  acts_as_shopping_cart_item
end

@muhammadn
Copy link

Unless you need the column fields like :owner_id, :owner_type, :quantity, :item_id, :item_type, :price

to be accessible by the controller, you don't actually need attr_accessible for those fields.

My code:

class ShoppingCart < ActiveRecord::Base

  acts_as_shopping_cart
  # attr_accessible :title, :body

  def tax_pct
    0.0
  end
end

class ShoppingCartItem < ActiveRecord::Base

  acts_as_shopping_cart_item
  # attr_accessible :title, :body 
end

The items in the shopping cart are stored internally by the gem and there is no need for attr_accessible.

@pailoro
Copy link
Author

pailoro commented Aug 19, 2014

Right. Thanks!

But now, when I clik on

<%= link_to 'Add to cart', shopping_cart_path(:product_id => @v.id), :method => 'POST' %>

when I'm running and it redirect to shoping cart page, the item its not added to my cart. =(

@muhammadn
Copy link

Should be

<%= link_to 'Add to cart', shopping_cart_path(:product_id => @product), :method => 'POST' %>

as your create action to add the item to cart is:

def create
    @product = Video.find(params[:product_id])
    @shopping_cart.add(@product, @product.price) # the error is generated on that line
    redirect_to shopping_cart_path
end

@pailoro
Copy link
Author

pailoro commented Aug 20, 2014

But my controller to this is:

# encoding: utf-8
class ComprarController < SuperSiteController

   def index
    @v = Video.order('created_at asc')
    @n = News.all
    @cat = Category.all

    respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @v }
      end

 end

  def show
    @v = Video.find(params[:id])
    @n = News.all
    @cat = Category.all

    respond_to do |format|
              format.html # show.html.erb
               format.json { render json: @v }
           end

       end
    end

@muhammadn
Copy link

In your ShoppingCartController,

there is

def create
   @product = Video.find(params[:product_id])
end

@pailoro
Copy link
Author

pailoro commented Aug 22, 2014

Hi!!

Thnaks by the gelp!!

I'm getting it here:

Couldn't find Video without an ID

class ShoppingCartsController < SuperSiteController
  before_filter :extract_shopping_cart
  def create
    @product = Video.find(params[:product_id])
    @shopping_cart.add(@product, @product.price)
    redirect_to shopping_cart_path
  end
  def show

error

@pailoro
Copy link
Author

pailoro commented Aug 22, 2014

And when I change the add link from:

<%= button_to 'Add to cart', shopping_cart_path(:product_id => @product), :method => 'POST' %>

to:

<%= button_to 'Add to cart', shopping_cart_path(:product_id => @v.id), :method => 'POST' %>

I get the error above:

error2

@pailoro
Copy link
Author

pailoro commented Aug 22, 2014

I saw the log of passenger on my online server and it is returning this:

App 23916 stdout: Started GET "/comprar/gary-semics-mx-vol-01" for 189.38.85.9 at 2014-08-22 10:04:28 -0300
App 23916 stdout: Processing by ComprarController#show as HTML
App 23916 stdout:   Parameters: {"id"=>"gary-semics-mx-vol-01"}
App 23916 stdout:   Rendered comprar/show.html.erb within layouts/site (30.7ms)
App 23916 stdout: Completed 500 Internal Server Error in 37.8ms
App 23916 stdout: 
App 23916 stdout: ActionView::Template::Error (undefined method `shopping_cart_path' for #<#<Class:0x00000003cc4ae0>:0x00000003af1f38>):
App 23916 stdout:     112: 
App 23916 stdout:     113:            
App 23916 stdout:     114: 
App 23916 stdout:     115:               <%= button_to 'Add to cart', shopping_cart_path(:product_id => @v.id), :method => 'POST' %>
App 23916 stdout:     116: 
App 23916 stdout:     117: 
App 23916 stdout:     118: 
App 23916 stdout:   app/views/comprar/show.html.erb:115:in `_app_views_comprar_show_html_erb__1444626235673011822_22184820'
App 23916 stdout:   app/controllers/comprar_controller.rb:29:in `show'

I'm using the gem "FriendlyId" for permalinks. i think it should be the cause of the error, incompatibility?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants