I've been trying to add to a value in my DB, rather than replace it.
If you were to input 5 as the value in the update/edit form, I want the DB to be 5 plus whatever is already in the DB. So, if the DB already has 1 refill, entering 5 in the form would result in 6 in the DB.
When putting different :values into the update form I have been breaking the form, and a rails error comes up. Not sure if/how to pass a function in the input of a ruby form. This project is mainly written in Ruby(2.4.1p111) on Rails (5.1.5). Below is the Controller
def update
@waterbottle = Waterbottle.find(params[:id])
if @waterbottle.update(waterbottle_params)
redirect_to "/users/#{current_user.id}"
end
end
Schema
create_table "waterbottles", force: :cascade do |t|
t.integer "volume"
t.integer "user_id"
t.integer "refills"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
And the update form is.
<%= form_for @waterbottle, :url => waterbottle_path(@waterbottle.id) do |f| %>
<p> How many times did you refill your bottle today? </p>
<%= f.text_field :refills, :value => @waterbottle.refills %>
<%= f.hidden_field :user_id, :value => current_user.id %></br>
<%= f.hidden_field :volume, :value => @waterbottle.volume %></br>
<%= f.submit "Refill Waterbottle" %>
<% end %>
Any help is greatly appreciated.
:url => waterbootle_patheither, justform_for @waterbottle do |f|- again, rails magic.