Reputation: 33
I'm having trouble updating columns in my table with a button in Rails. I've looked around stackoverflow for answers, but can't seem to get anything working...
In my app, I want users to be able to "rent" and "return" pins. When they click the "Rent" button, I want it to change the pin's columns.
Example: rented: => true, renter_id: 1"
In my Pins controller:
def rent
@pin = Pin.update_attribute(:renter_id => @user.id)
end
In my View:
<%= button_to "Rent Now!", {:controller => "pins", :action => "rent", :renter_id => @user.id }, :class => 'btn btn-success btn-lg btn-block'%>
Schema:
create_table "pins", force: true do |t|
t.string "description"
t.string "link"
t.boolean "rented"
t.integer "renter_id"
end
Upvotes: 1
Views: 288
Reputation: 983
One problem is that update_attribute
is an instance method. But you are trying to call it as a class method on Pin
. Perhaps do a controller like so.
def rent
@pin = Pin.find(params[:id])
if @pin.update_attributes(renter_id: @user.id)
#handle success
else
#handle failure
end
end
Also make sure that your routes are properly set up as a post request.
Upvotes: 3
Reputation: 29094
update_attribute takes 2 arguments, the attribute and the value
So it should be like this
update_attribute(:renter_id, @user.id)
If you want to update multiple attributes at once or if you want validations to be triggered, use update_attributes
update_attributes(:attr1 => value1, :attr2 => value2)
Upvotes: 2