Take the 2-minute tour ×

I am having trouble understanding as how do you trigger specific button event when using Ruby on Rails. For example I have a mini form inside a table as so (View side of MVC):

<%= form_tag do %>
<% if student.floor_pref == '1st' %>
    <td><%= select_tag 'room', options_for_select(@first_floor.map { |value| [value,value]}, @selected_room) %></td>
<% end %>
<% if student.floor_pref == '2nd' %>
    <td><%= select_tag 'room', options_for_select(@second_floor.map { |value| [value,value]}, @selected_room) %></td>
<% end %>
<% if student.floor_pref == '3rd' %>
    <td><%= select_tag 'room', options_for_select(@third_floor.map { |value| [value,value]}, @selected_room) %></td>
<% end %>

<td><%= submit_tag 'Update' %></td>
<% end %>

How do I tell the controller that when the Update button is clicked inside the view to for example execute this code:

a = Student.find(3)

a.room_number = '2105'
share|improve this question
add comment

2 Answers

Your button should be triggering a post-back to the server. You need to define an action in your controller, a route to reach that action, and set your forms action attribute to that route. Your action can then update the models and respond with a success or error message.

share|improve this answer
Can you show me how this is done using this example? I am new to Ruby and asked this before but no one can seem to help. –  Nick Jun 14 '13 at 16:18
Which part? I gave you three steps: Define an action in your controller, create a route, update your <form> tag to include an action="..." attribute to reach your route. This is extremely basic Rails coding, and literally every tutorial written will tell you how to do this. –  meagar Jun 14 '13 at 16:19
All three parts really. I've used scaffolding to build my MVC and besides just declaring variables that run a query and return results inside a controller I really don't understand much beyond that point. I've gone through couple of tutorials and they all show the same thing which really doesn't help me with this. –  Nick Jun 14 '13 at 16:25
This is just basic example that is a part of bigger problem that I'm trying to figure out. I feel that if I can get this working I can build on that and use the stuff I learned from tutorials to solve the problem. –  Nick Jun 14 '13 at 16:27
add comment

u should specify a route(a controller action to which the data is to be submitted) in the form tag. refer http://apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag for more details. considering that ur action is for 'edit' action from students controller and that data is to be posted to 'update', the view can be defined as

<%= form_tag student_path(@student), :method => :put do %>
  <$= submit_tag 'Update' %>

and in the 'update' method, u can write the code to update student with the received params. I think u need to study basic rails functioning since it will save u time in future. u can refer http://guides.rubyonrails.org/getting_started.html Also, prefer using form_for rather than form_tag.

share|improve this answer
Thanks for the quick response. In the form tag could I specify it as following: <%= form_tag :controller => 'students', :action => 'update' do %> ? Only reason I'm asking this way is because I'm still learning the Ruby syntax and this makes more sense to me. Thanks again. –  Nick Jun 18 '13 at 16:45
yes. please have a look at stackoverflow.com/questions/9540291/… for difference between form_for and form_tag. –  prasad.surase Jun 18 '13 at 16:51
Sweet, that got me somewhere. Thanks! Data is updating fine now but the action I call in the controller as shown above 'update' doesn't have a view so when I click the update button it actually updates the room column but after that it displays the "Template is missing" page. Is there a way that I can tell it after it perform the 'update' action to just go back to the 'index' page? Thanks again! –  Nick Jun 18 '13 at 16:59
Got it all working now, thanks a bunch surase!!! –  Nick Jun 18 '13 at 18:42
u can redirect to any page if the object is created/updated successfully. check point 6.9 on rails guides for an example. –  prasad.surase Jun 18 '13 at 18:46
show 1 more comment

Your Answer


