button_to (passing paramters again)

262 views
Skip to first unread message

Joe Guerra

unread,
Feb 15, 2017, 7:11:02 PM2/15/17
to Ruby on Rails: Talk
I am pretty close to figuring this out...

I've got this in my product show page....I'm trying to pass user_id & product_id to my products controller, add_to_cart method...


<%= button_to  'Add to Cart', {:controller => "products", :action => "add_to_cart", :user_id=> session[:user_id], :product_id => @id   } , :method=>:post  %>

on the controller...

def add_to_cart(user_id, product_id)


@cart = Cart.new(user_id, product_id )
#

end

I get the error, 

"wrong number of arguments (given 0, expected 2)"



Any suggestions?

Thanks,

Joe


Hassan Schroeder

unread,
Feb 15, 2017, 11:58:50 PM2/15/17
to rubyonrails-talk
On Wed, Feb 15, 2017 at 4:11 PM, Joe Guerra <JGu...@jginfosys.com> wrote:

> I've got this in my product show page....I'm trying to pass user_id &
> product_id to my products controller, add_to_cart method...

Accepting user_id as an untrusted input from the client is a
Very Bad Idea. If it's a value saved in session you can and
should fetch it in your controller.

> <%= button_to 'Add to Cart', {:controller => "products", :action =>
> "add_to_cart", :user_id=> session[:user_id], :product_id => @id } ,
> :method=>:post %>
>
> on the controller...
>
> def add_to_cart(user_id, product_id)
> @cart = Cart.new(user_id, product_id )
> end

> "wrong number of arguments (given 0, expected 2)"

So where are you setting user_id and product_id from the params?
I assume you've looked at the log and confirmed the params hash
contains what you expect?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Scott Jacobsen

unread,
Feb 16, 2017, 12:18:38 AM2/16/17
to Ruby on Rails: Talk
Controller action methods do not take parameters. You get the parameters from the `params` hash. Also, as was mentioned, do not send user_id as a param. It is a security error, and you don't need to because you can access the session in the controller:

def add_to_cart
  product_id = params["product_id"]
  user_id = session["user_id"]
  @cart = Cart.new(user_id, product_id)
  ...
end

Joe Guerra

unread,
Feb 16, 2017, 9:27:11 AM2/16/17
to Ruby on Rails: Talk
ok, still confused....   
I've defined my parms like this...

def cart_params
# params.fetch(:cart, {})
params.require(:cart).permit(:user_id, :product_id )


end

Have this in my controller.

def add_to_cart

product_id = params['id']

user_id = session['user_id']
@cart = Cart.new(user_id, product_id)

end


Not sure what to put in the show page, to pass these values to the controller.  

I've got a few rails books, and not one of them cover this.  

Walter Lee Davis

unread,
Feb 16, 2017, 5:51:29 PM2/16/17
to rubyonra...@googlegroups.com

> On Feb 16, 2017, at 9:27 AM, Joe Guerra <jgu...@jginfosys.com> wrote:
>
> ok, still confused....
> I've defined my parms like this...
>
> def cart_params
> # params.fetch(:cart, {})
> params.require(:cart).permit(:user_id, :product_id )
>
>
> end
>
>
> Have this in my controller.
>
> def add_to_cart
>
> product_id = params['id']
> user_id = session['user_id']
> @cart = Cart.new(user_id, product_id)
>
> end
>

Try this:

product_id = cart_params[:product_id]
user_id = session['user_id']
@cart = Cart.new(user_id, product_id)

Not clear what params[:id] would be set to in your example, but you went out of your way to whitelist cart[product_id], so that's what I think you should use.

Walter

>
> Not sure what to put in the show page, to pass these values to the controller.
>
> I've got a few rails books, and not one of them cover this.
>
>
>
> On Thursday, February 16, 2017 at 12:18:38 AM UTC-5, Scott Jacobsen wrote:
> Controller action methods do not take parameters. You get the parameters from the `params` hash. Also, as was mentioned, do not send user_id as a param. It is a security error, and you don't need to because you can access the session in the controller:
>
> def add_to_cart
> product_id = params["product_id"]
> user_id = session["user_id"]
> @cart = Cart.new(user_id, product_id)
> ...
> end
>
> On Wednesday, February 15, 2017 at 5:11:02 PM UTC-7, Joe Guerra wrote:
> I am pretty close to figuring this out...
>
> I've got this in my product show page....I'm trying to pass user_id & product_id to my products controller, add_to_cart method...
>
>
> <%= button_to 'Add to Cart', {:controller => "products", :action => "add_to_cart", :user_id=> session[:user_id], :product_id => @id } , :method=>:post %>
>
> on the controller...
>
> def add_to_cart(user_id, product_id)
>
>
> @cart = Cart.new(user_id, product_id )
> #
>
> end
>
> I get the error,
> "wrong number of arguments (given 0, expected 2)"
>
>
>
>
>
> Any suggestions?
>
> Thanks,
>
> Joe
>
>
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f62d02c7-7059-425b-b810-cb28a8d8f392%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Joe Guerra

unread,
Feb 28, 2017, 10:45:31 AM2/28/17
to Ruby on Rails: Talk
I finally got it working.  I went to a local ruby on rails meetup and got some help on the issue.

I needed to use current_user.id and params[:product_id]  for my parameters in my function.

Thanks,
Joe
Reply all
Reply to author
Forward
0 new messages