Ilia
Ilia

Reputation: 101

How do I change the boolean value to true on button click?

I am building a rails app and am struggling to change a column value to 'true'.

In my view:

<%= link_to "Accept", accept_path(jobapp), class: "button-card-applicant" %>

As you can see below, I have a column called ACCEPTED?.

  create_table "jobapps", force: :cascade do |t|
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.bigint "user_id", null: false
    t.bigint "job_id", null: false
    t.text "message"
    t.boolean "accepted"
    t.index ["job_id"], name: "index_jobapps_on_job_id"
    t.index ["user_id"], name: "index_jobapps_on_user_id"
  end

When a user clicks the ACCEPT BUTTON, I intend to run this code in my controller:

  def accept
    @accept = Jobapp.find(params[:id])
    @accept.update_attribute(accepted: true)
    redirect_to applicant_path(current_user)
  end

However, my db is showing a value of nil (removed some info to make it easy to read):

#<Jobapp id: 9, accepted: nil>

Lastly, I have defined my routes as follows:

  resources :jobapps, only: [] do
    get :accept, on: :member
  end

So I think my routes is looking incorrect but I wanted to check as I am still getting the hang of rails. But my main question is how do I change the boolean value to true?

Hope this is enough info, will update question if more is required. Thanks for your help!

UPDATE

Some rookie errors were corrected in this question but the problem remains the same.

Upvotes: 1

Views: 928

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14890

The attribute is named accepted, not accepted?, you should change your column name to be accepted. Rails will automatically create the accepted? method for you then.

Also, update_attribute takes 2 parameters, the attribute and the new value.

Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Also note that

Validation is skipped.

Callbacks are invoked.

updated_at/updated_on column is updated if that column is available.

Updates all the attributes that are dirty in this object.
@accept.update_attribute(:accepted, true)

Upvotes: 2

Related Questions