Obviously the rhtml below does not work, but it should be apparent what
I try to achieve - I would like to give the user the choice to submit a
form either to action1 or action2 but I cannot figure out how?! It would
be great if someone could give some assistance.
Thanks for the reply. Is there maybe another way and use in the view:
and then use
if params[:save]
else if params [:send]
end
in the controller? The problem I encounter here is that the <button…>
is just a funny little button and not a real button (with name…like
the ‘Submit’ just below) which I would require…I guess this is an
HTML issue. What would you suggest?
Thanks, Alex
liang gao wrote:
you probably have 2 methods like in your controller
def save
end
def saveandsend
end
You may need to define 2 views like save.rhml and saveandsend.rhtml,
instead of just one view as you stated.
Also, a form can only have one action, it is not a RoR rule, but a HTML
rule.
And here is a stripped-down version of what I did to
get action variations driven by which button is selected.
Basically, users are able to change the state or remove items in
some table rows (not shown). One column had checkboxes to
indicated which rows are to be affected).
def update_list
…
…
case params[:_update_button]
when “Enable Selected”: enablement = true
when “Disable Selected”: enablement = false
when “Remove Selected”:
# do the remove thing
else
flash[:notice] = “That button is not hooked up properly.”
end
…
…
end
Alternatively, a simple solution is to name all of the action buttons
the same but set the value attribute to distinguishing values. So, in
the rhtml file you would have:
In the item_controller.rb file then you can access the parameter
submit_action and take the appropriate action
@action=@params[‘submit_action’]
if (@action == ‘Add’) then
…blah, blah
else
…blah, blah
end
I find it easiest to specify unique names for each submit button. You
avoid name collisions this way, plus, the only submit name/value that
gets posted is the one that is clicked.