I am new to Ruby on Rails and I want to create a very simple application. I used scaffolding to create a database called users. Two of the fields in users are
<%= link_to 'Edit', edit_user_path(@user) %>
<%= link_to 'Take container', #run method to decrease and return here# %>
<%= link_to 'Return container', #run method to increase and return here# %>
AJAX would be a good fit here, but that would complicate the question, so I will stick to doing it with Rails.
You will first need to edit your routes.rb file to include something like:
get '/remove_container/:user_id', to: 'users#remove_container', :as => :remove_container get '/add_container/:user_id', to: 'users#add_container', :as => :add_container
Then add methods in your Users controller:
def remove_container user_id = params[:user_id] @user = User.find user_id # code to change container number redirect_to edit_user_path(@user) end def add_container user_id = params[:user_id] @user = User.find user_id # code to change container number redirect_to edit_user_path(@user) end
There are a lot of things to make this better, but this should get you going in the right direction anyway.
Links to these could be made manually or done with paths:
<a href="/remove_container/[:user_id]">Remove Container</a>
link_to "Remove Container", remove_container_path(@user)