views:

38

answers:

1

I need to have multiple submit buttons.

I have a form which creates an instance of Contact_Call.

One button creates it as normal.

The other button creates it but needs to have a different :attribute value from the default, and it also needs to set the attribute on a different, but related model used in the controller.

How do I do that? I can't change the route, so is there a way to send a different variable that gets picked up by [:params]?

And if I do then, what do I do in the controller, set up a case statement?

+4  A: 

You can create multiple submit buttons and provide a different value to each:

<% form_for(something) do |f| %>
    ..
    <%= f.submit 'A' %>
    <%= f.submit 'B' %>
    ..
<% end %>

This will output:

<input type="submit" value="A" id=".." name="commit" />
<input type="submit" value="B" id=".." name="commit" />

Inside your controller, the submitted button's value will be identified by the parameter commit. Check the value to do the required processing:

def <controller action>
    if params[:commit] == 'A'
        # A was pressed 
    elsif params[:commit] == 'B'
        # B was pressed
    end
end

However, remember that this tightly couples your view to the controller which may not be very desirable.

Anurag
Now thats something new. Thanks @Anurag!
Shripad K
so just putting the 'A' automatically create parameter name='commit'?
Angela
is there a way as you said not to tightly couple the view to the controller? for example, for the submit buttons to change the URL? It *seems* that this isn't necessarily bad because a form submits variables which can change the behavior of hte controller,e ven if it user input, which the selection of the button is?
Angela
You can't change a form action attribute without a messy js hack.
benoror
Changing the form action attribute on the fly is a more brittle solution. Using the commit attribute is less so. You could as an alternative wrap the second submit button inside a different form and pass a parameter that needs to be changed to the same action. But it is not much different than relying on the values of the 2 submit buttons. Without knowing more how you've setup this thing, the best solution so far would be the with 2 submit buttons.
Anurag
okay good...I'm fine with this, just wanted to know, will experiment with it hopefully today and let you know!
Angela
Cool, so far so good....awarded!
Angela