button_to question

Hi,

I’m trying to run a method in one of my controllers using a button_to command. How is this accomplished?

So far, I’ve got <%= button_to “Add to Cart”, action: “add_to_cart” %> in my products page

I defined a add_to_cart in my product controller, and created a route for it.

Am I on the right track? Will it execute code in that method in that controller?

Thanks,

Joe

Why don’t you try it out :slight_smile: Yes it must work, but are you passing the product / item id to be added and the quantity to be added?

Hi,

I'm trying to run a method in one of my controllers using a button_to command. How is this accomplished?

So far, I've got <%= button_to "Add to Cart", action: "add_to_cart" %> in my products page

I defined a add_to_cart in my product controller, and created a route for it.

Am I on the right track? Will it execute code in that method in that controller?

Try it and see, that would be a lot quicker than asking here. If it doesn't then look in log/development.log and/or the server terminal window to get a clue as to why not. When setting the route don't forget that button_to will use POST.

Colin

ok, it’s looking for a view at that route (or path), but my method says to render a partial file (modal dialog page).

Hmmm, did I miss something?

ok, it's looking for a view at that route (or path), but my method says to render a partial file (modal dialog page).

Hmmm, did I miss something?

Without more information it is impossible to say.

Colin

OK, maybe I just need to see an example of how to call a method from a button-to button.

OK, maybe I just need to see an example of how to call a method from a button-to button.

No you need to show us what you have done and tell us what is not working. Which bit is failing? Is it getting to your controller action? Is it failing running the action? Is it not rendering the next view correctly? Or what. Show us the specific bit of code that is not working.

Colin