Reputation: 43
I used rails generate scaffold vendor expresso_count:integer
I now want to make a button that when clicked updates expresso_count for that vendor from the show.html.erb view that gets generated
But I have two problems.
problem A) when using this code, it works fine and will update:
<% @vendor.update(expresso_count: '10' ) %>
but this code does not, and its what I want, so that I can just increment the expresso_count by 1. What it current does is just set the value to 0e
<% @vendor.update(expresso_count: '@vendor.expresso_count+1' ) %>
Problem B) How do I put that into a button_to such that I can have a button which when pressed, will increment @vendor.expresso_count by 1
Here is a link to my git repo (which will be a bit out of date) https://[email protected]/umlungu/mybeans.git
I'm still very new to Rails and learning as I go. so would appreciate any help.
Thanks :D
Upvotes: 0
Views: 45
Reputation: 58435
Firstly, let me heartily recommend Michael Hartl's rails tutorial here: https://www.railstutorial.org/ - I'm also new to Rails and it is excellent.
The first thing to understand is the MVC framework that rails uses. Models hold you data, Views are how you interact with your data, Controllers are the glue between them.
So you want a view that is going to make your controller do something to your model. Currently you're trying to do this from a view.
I would put a link in a view to /vendor/#{@vendor.id}
and POST
some kind of data indicating that I am wanting to increment my expresso_count. In my view, I would have something like:
def update
#assuming all I do is increment my count
Vendor.find(param[:id]).expresso_count += 1
end
Upvotes: 1