1

In my views/users/edit.html.erb, I have the following:

<%= form_for @user do |f| %>
.........
  <div class="controls"><%= submit_tag "Update User Info", :class => "submit_button" %></div>
<% end >

When that button is clicked, it's sent to the controller's update method. How do I get it to go to another controller method? I looked up the documentation for ActionView, and nothing jumped at me. Suggestions?

1 Answers1

3
<%= form_for @user, :url => some_other_route_path do |f| %>

Simples! You can also, if you like, do:

<%= form_for @user, :url => {:controller => :users, :action => :my_action} do |f| %>

More info

Matt
  • 13,948
  • 6
  • 44
  • 68
  • 1
    Since the OP is a beginner, according to their username, [this link](http://guides.rubyonrails.org/routing.html#paths-and-urls) points to the rails documentation on route paths, to help further explain the first suggestion, in case it was not already known. – Paul Richter Jun 12 '13 at 18:39
  • I did read the section on routes and paths, but I'm still a little confused. :my_action would be the method name in the users controller, or something like update_user_info_user. The later did not work. ActionConttoller told me that there is no matching route. Would I have to explicitly define that route in routes.rb? Reading through the documentation, it seemed as if the action controller would take care of it without specific entry in routes.rb. I was also wondering if this can be done through a submit button syntax, as opposed to form_for @user ... do – Rails Beginner Programmer Jun 12 '13 at 19:09
  • You will need to either define the specific route in routes.rb (preferred) or allow routes to match based on format - there is a line at the end of the generic routes.rb that does this, you just need to uncomment it (not preferred until you understand the repercussions of using it). In short, define a route! – Matt Jun 12 '13 at 19:12
  • It seems as if uncommenting that last line would break the REST model. I added a specific route in routes.rb and it works fine now (I still have to work out a small problem of passing the user id, but that seems easy to do). It's too bad that I can't direct it to a specific route through a submit button action. That way, I can have multiple submit buttons, each with a specific action, resulting in simpler controller methods. – Rails Beginner Programmer Jun 12 '13 at 19:27
  • You can have different behaviours on different buttons by giving the buttons a `name` attribute, then checking the `commit` parameter in the controller, it will contain the name of the button. – Matt Jun 12 '13 at 19:41
  • Thank you, but that's the approach I took. I was wondering if there was another approach. – Rails Beginner Programmer Jun 13 '13 at 17:38
  • You could use javascript to change the url of the form, if you wanted to. [This question](http://stackoverflow.com/questions/979024/changing-the-action-of-a-form-with-javascript-jquery) can get you started. – Matt Jun 13 '13 at 20:38