0

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.

4
  • Post the code you're trying that gives you an error, and also post the error. Commented Apr 4, 2018 at 0:30
  • also: hidden_field can be easily hacked/modified, so you should do anything you need with current_user.id in the controller, instead of passing the value with the form. Commented Apr 4, 2018 at 0:38
  • finally; you don't need the :url => waterbootle_path either, just form_for @waterbottle do |f| - again, rails magic. Commented Apr 4, 2018 at 0:39
  • I took the liberty of editing your post to make it more clear what you're asking. Commented Apr 4, 2018 at 0:52

2 Answers 2

1

Offering you to use before_update callback in your model. And you can get old values by refills_was method.

class Waterbottle < ApplicationRecord
  before_update :increase_fills

  def increase_fills
    if self.refills_changed?
      self.refills = self.refills + self.refills_was
    end 
  end
end

Hope this will help you to solve problem.

Sign up to request clarification or add additional context in comments.

Comments

1

you can easily do math or other functions inline in the value part:

<%= f.text_field :refills, :value => (@waterbottle.refills + 1) %>

Bear in mind, generally you don't need to specify the value if you're not changing it. (I made a few other stylistic and security comments, above, about your other rails code. good luck!)

1 Comment

I believe you misunderstood the OP's intent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.