1

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'
a.save

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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.
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.
2
+50

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.

5 Comments

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.
yes. please have a look at stackoverflow.com/questions/9540291/… for difference between form_for and form_tag.
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!
u can redirect to any page if the object is created/updated successfully. check point 6.9 on rails guides for an example.
I see, thanks. I'm going over that guide atm. Thanks again for your help, it helped me solve the big problem I was originally having. Now the web app works 100%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.