2016-07-08 60 views
-2

我想使用link_to更新任務 的is_completed布爾值,聯繫人有很多任務。rails 4更新布爾值使用link_to

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 
======== 

如何能做到這一點的接觸/指數

回答

0
# 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有一定的限制,或者:

contact.is_completed = true 
contact.save