rookieRailer
rookieRailer

Reputation: 2341

Multiple Submit button in a rails form

I am trying to implement a "Preview" feature before creating an object for a product. I followed Ryan Bates railscast on doing so, in which he does so by passing a parameter :name with the submit button.

<%= submit_tag 'Preview', :name => 'preview_button' %>

However, it's not working in my case, don't know why, may be because I'm using rails 3, I can't tell, but I do not see any changes when I add :name => 'preview_button' with the submit button. Can anyone suggest me a solution?

Upvotes: 0

Views: 1057

Answers (2)

siliconsenthil
siliconsenthil

Reputation: 1460

Adding Paulo's answer, it might be better to route to different actions based on commit param.

We solved using advanced constraints in rails.

The idea is to have the same path (and hence the same named route & action) but with constraints routing to different actions.

resources :plan do
  post :save, constraints: CommitParamRouting.new("Propose"), action: :propose
  post :save, constraints: CommitParamRouting.new("Finalize"), action: :finalize
end

CommitParamRouting is a simple class that has a method matches? which returns true if the commit param matches the given instance attr. value.

This available as a gem commit_param_matching.

Upvotes: 3

Paulo Fidalgo
Paulo Fidalgo

Reputation: 22331

You need to add two submit buttons with different names

<%= submit_tag 'Submit', :name => 'submit_button' %>
<%= submit_tag 'Preview', :name => 'preview_button' %>

then in your controller you need to check the parameters:

if params[:commit] == "Submit"
# do great stuff
end
if params[:preview] == "Preview"
# preview great stuff
end

Upvotes: 0

Related Questions