0

I have a rails form that creates (and updates) materials. Obviously, by default, when the user is logged in and the materials is retrieved the form renders with the update action. However, I want to have a "save as" action in case the user wants to save another version of the material. The obvious way to do this is to have one button which sends to 'update' in the controller and another which sends to 'new', but I don't know how to do this since it seems to depend on the form_for parameters.

Update

To avoid using Javascript I tried changing the form_for url to:

<%= form_for @material, :url => choose_action_path do |f| %>

Then in the controller I have:

def choose_action
  if params[:save_as] == "Save As"
    redirect_to :action => "create"
  else
    redirect_to :action => "update"
  end
end 

but this is not working. Is this a strategy that could work or is this crazy?

Finnjon
  • 651
  • 4
  • 19
  • Sounds like a bad idea. How about handling the form appropriately on the server side only? You could delegate to wherever you need to from one controller. That form would be too easy to mess with in the browser. – vector Mar 05 '13 at 22:24
  • You could add a checkbox to know if the user wants to create a new version or to save the actual one. And on the server side, just check if the checkbox has been checked and delegate to the appropriate method. – TimPetricola Mar 05 '13 at 22:27
  • By redirecting you loose all the data you posted from the form. So your `choose_action` idea is indeed *crazy*. – Mischa Mar 06 '13 at 13:09
  • @Mischa Yes I was worried about that. Is there no way to pass the data along to the next action? – Finnjon Mar 06 '13 at 13:25

1 Answers1

0

You could probably do this with some jquery.

Let's say the model you are working on is called Email. When you edit an Email, the form tag that is created with have an action action='/emails/:id and a method method='post'. Hidden underneath the form tag is something like

<form accept-charset="UTF-8" action="/email/1" method="post">
  <div style="margin:0;padding:0">
    <input name="_method" type="hidden" value="put" />
    <input name="utf8" type="hidden" value="&#x2713;" />
    <input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
  </div>

more info can be found here: http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-put-or-delete-methods-work

That extra chunk of code makes the form submit a put request instead of a post, which subsequently maps to the update action of your controller. To get it to send the form to the create action, you want to post the action "/email". In other words, you want to manipulate the code above into something like

<form accept-charset="UTF-8" action="/email" method="post">

Note the removal of the div tag.

So my suggestion would be to create a button "Save As", then add some jquery which will make those modifications when the "Save As" button is clicked.

This stackoverflow answer might help you with the jquery Jquery to change form action

Community
  • 1
  • 1
cgat
  • 3,689
  • 4
  • 24
  • 38