How to change an attribute in the bank via a button

Asked

Viewed 101 times

2

I’m creating a small college project in Ruby on Rails and I came across a problem: I have a table called Person and another call Tools. People have many tools and every tool and owned by one person.

I need to add a feature that allows the loan of tools between people in the system. For this, I created an attribute called 'loan' on the table Tools, that if it has the value 0 means the tool is not borrowed and if it is 1, that is borrowed. In the view show of the tools, I created a button with the function to borrow the tool.

My problem is how to make this button change the attribute 'loan' table Tools of 0 for 1 and vice versa? Someone would have a better solution?

1 answer

1


You can do an action on ToolsController, where you will pass the id of the tool, in this controller you perform the lending action.

I suggest you instead of putting an attribute loan, put the user_id of the user who made the loan, so you can better control this relationship. That is to say:

  • user_id with id == borrowed tool for user
  • user_id without id == tool free for loan

In the action it would be enough for you to associate the current_user à tool referent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.