0

Possible Duplicate:
Ruby on Rails: How to have multiple submit buttons going to different methods (maybe with with_action?)

In a form I have some submit_tags and on server side I have to detect which one was clicked. This is that I've tried, not working: on server side I only get an action name in params:

<%= form_tag controller_action_path(:id => @project.id), :method => :post do %>
    <% if @project_is_synced %>
        <%= submit_tag 'Update synchronization', :name => 'update' %>
        <%= submit_tag 'Stop synchronization', :name => 'stop' %>
    <% else %>
        <%= submit_tag 'Start synchronization', :name => 'start' %>
    <% end %>
<% end %>

I have only params[:action] with a current action name which is always the same

Community
  • 1
  • 1
Dmitry Dyachkov
  • 1,715
  • 2
  • 19
  • 46

1 Answers1

1

The easiest way to debug this is to run it locally and look at the parameters as they come through, or log parameters in your action:

 if request.post?
    logger.warn "POST #{params}"

 end

You have named the submit_tags, so instead of the default name 'commit', each button has a different name, and you'll have to check for params named 'start', 'stop', or 'update'.

Simplest is just to remove the names and check params[:commit] for a varying value, however if you don't want to change the view, use this (replacing the render code obviously):

  if params[:start]
    render :text => "start"
  elsif params[:update]
    render :text => "update"
  else
    render :text => "stop"
  end

The comments to the answer in the linked post do deal with this, but I can see why you missed it.

Kenny Grant
  • 9,360
  • 2
  • 33
  • 47
  • Parameters were: {"utf8"=>"✓", "authenticity_token"=>"fhtuzoK/U7KkZ8Fm/Wj5bIscoO2tEhPxJMQw0AK1O6I=", "project_name"=>"Enter project url", "releases"=>"true", "events"=>"true", "usecases"=>"true", "id"=>"1"} – Dmitry Dyachkov Oct 16 '12 at 20:31
  • Looks as if this problem: http://stackoverflow.com/questions/2000111/rails-is-not-passing-the-commit-button-parameter – Dmitry Dyachkov Oct 16 '12 at 20:33
  • 1
    Your params do not match the form posted above as the view, and somehow the commit buttons are being removed, so if you disable whatever is doing that the code above should work for you (NB the note about naming your commit buttons still applies) – Kenny Grant Oct 16 '12 at 20:36
  • I've found out that submit_tag was overriden in application_helper by another developer, what a pity to lose time because of that. Thanks for help. – Dmitry Dyachkov Oct 16 '12 at 20:55
  • ah ok. Monkey-patching built-in methods is really not a good idea, I hope you told them off! – Kenny Grant Oct 16 '12 at 21:07