当前位置: 动力学知识库 > 问答 > 编程问答 >

rails 4 update boolean using link_to

问题描述:

I want to use the link_to update the is_completed boolean of the task

contact has many tasks.

resources :contacts do

resources :tasks

end

===============

create_table "tasks", force: :cascade do |t|

t.text "content"

t.date "due"

t.boolean "is_completed", default: false

t.datetime "created_at", null: false

t.datetime "updated_at", null: false

t.integer "contact_id"

end

========

how can do that in the contact/index

网友答案:
# in your view
<%= link_to 'make it complete', contact_task(@contact, @task), method: :patch %>

# in your controller
def update
  contact = Contact.find(params[:id])
  contact.update_attribute(:is_completed, true)
  # ...
end

update_attribute has some restriction, alternatively:

contact.is_completed = true
contact.save
分享给朋友:
您可能感兴趣的文章:
随机阅读: