DougN
DougN

Reputation: 337

Rails: Two separate create forms..how to keep them separate?

In Rails 3.0 I have the standard 'new' form that creates a new record, in this case a patient. It works fine and the validations / error showing also work fine.

The client now wants the form in Spanish.

So, I did this:

  1. Created a new html doc called "newspanish" (Cut / paste code from "patients/new")
  2. Created a new partial called "_form_newspanish" and referenced it where the "form" partial is in "newspanish" (Cut / paste code from view "patients/_form")
    1. Created a controller action in "patients" called "newspanish" and cut/pasted exact code from the "new" action.
    2. I left the "create" action untouched.
  3. Added match "patients/newspanish" to routes.
  4. Translated the english parts to spanish in views/newspanish and views/_form_newspanish. Just the stuff that users read on the page, of course...not the rails code.

And, it works, for perfect submissions.

For submissions that fail validation (like putting 3 digits in as a phone number), the page reverts to the view "patients/new" and shows the errors above the form... in English, of course, because patients/new is in English.

Of course, I want it to revert to "views/newspanish" and also show custom verbage in the validations errors (spanish).

Any thoughts on how I can get the patients/newspanish view to load when error validation it tripped?

Here's my code for "_form_newspanish"

<%= form_for(@patient) do |f| %>

<% if @patient.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@patient.errors.count, "error") %> prohibited this subscriber from     being saved:</h2>

      <ul>
      <% @patient.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


  <p><label for="mobile">Número de teléfono celular o  móvil*</label>: <%= f.text_field :mobile %></p>
  <br />
    <%= f.submit "Inscribirme" %>
  </div>
<% end %>

And controller... patients/newspanish

  def newspanish
    @patient = Patient.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @patient }
    end
  end

Upvotes: 0

Views: 75

Answers (1)

Josh Crews
Josh Crews

Reputation: 799

<%= form_for(@patient) do |f| %>

is creating a form whose url is submits to is "/patients" which matches to patients_controller, create action.

That create action probably has a line that says (in my pseudo code)

if @patient.save
 redirect to somewhere
else
 render :new
end

That line "render :new" is showing the "patients/new" view.

So what you have to figure out is to either 1) detect in patients_controller # create how to tell if its spanish, and render "newspanish"

OR

2) change <%= form_for(@patient) do |f| %> to submit to a new url that just handles the spanish version, and make a new controller or action that just handles the spanish form (and renders "newspanish" if the @patient doesn't save

For #2, you could manually change where the form submits to with

<%= form_for(@patient), :url => spanish_patients_path do |f| %>

and in your routes create

post "patients/spanish" => "patients#create_in_spanish"

and add def create_in_spanish to your patients controller

Upvotes: 1

Related Questions