Forum: Ruby on Rails form_for, submit, and parameters disappearing

Posted by Ralph Shnelvar (ralphshnelvar)
on 2010-03-06 19:36
Consider the following:
Posted by Ralph Shnelvar (ralphshnelvar)
on 2010-03-06 19:50
Consider the following:

- - -
<% form_for @countryToSearchFor, :url => signed_in_path(:action => 
"show",
        :id => 'connected_to_country', :something => 'other'),
        :html => {:method => :get} do |f| %>

  <%= f.select :country, language_neutral_array_of_countries,
        :prompt => 'enter_country' %>
  <div>
    <%= f.submit shnI18n.literal('search_for_connection') %>
  </div>
<% end %>
- - -

The debugger tells me that
  signed_in_path(:action => "show",
    :id => 'connected_to_country', :something => 'other')
  # ==>  "/signed_in/connected_to_country?locale=en&amp;something=other"

When the user submits the form both the parameters (locale and 
something) have disappeared.  This causes more than a small bit of 
havoc.

How can I pass parameters via a submit to a controller via params?
Posted by Colin Law (Guest)
on 2010-03-06 22:35
(Received via mailing list)
On 6 March 2010 18:50, Ralph Shnelvar <lists@ruby-forum.com> wrote:
>  <div>
> When the user submits the form both the parameters (locale and
> something) have disappeared.  This causes more than a small bit of
> havoc.

I think maybe you cannot include query terms in form_for :url.  An
alternative is to use hidden fields in the form.

Colin
Posted by Ralph Shnelvar (ralphshnelvar)
on 2010-03-07 12:00
Colin Law wrote:
> I think maybe you cannot include query terms in form_for :url.  An
> alternative is to use hidden fields in the form.

Colin, thank you.   You appear to be correct.

So for the next poor person fighting to get internationalization to work
(I18n.  See http://guides.rubyonrails.org/i18n.html Section 2.3 Setting
and Passing the Locale) ..

form_for cannot be, uh, "internationalized" because the various flavors
of url_for that I18n hooks will have the locale parameter disappear if a
form, any form, is used. It appears not to be an ruby/rails/I18n problem
but an arcane "problem" in html.

Thus ... if you want to pass a couple of extra parameters when using
form_for ... you can do what I did.  I'm pasting in my adlterated code:

- - - - - - -
<% form_for @countryToSearchFor,
     :url => signed_in_path(:action => "show", :id =>
'connected_to_country'),
     :html => {:method => :get} do |f| %>
  <input type="hidden" name="locale" value="<%= params[:locale] %>" >
  <input type="hidden" name="somename" value="someothervalue" >
  <%= f.select :country,
               language_neutral_array_of_countries,
               :prompt => shnI18n.literal('enter_country') %>
  <br />
  <br />
  <div>
    <%= f.submit shnI18n.literal('search_for_connection') %>
  </div>
<% end %>
- - - - - - -

You will note the
  <input type="hidden" name="locale" value="<%= params[:locale] %>" >
  <input type="hidden" name="somename" value="someothervalue" >

The first <input> is the critical one where the locale is being passed.
The second <input> simply shows how to pass :somename =>
"someothervalue".

Both parameters (locale, somename) will end up in the params received by
the relevant controller.

- - -

I find using raw html to be far far easier to understand and more
convenient to use than the hidden_field api documented in
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html.

- - -

I find it amusing that the spam filter does not like the (proper 
spelling of) the word "adlterated"
Posted by Colin Law (Guest)
on 2010-03-07 14:37
(Received via mailing list)
On 7 March 2010 11:00, Ralph Shnelvar <lists@ruby-forum.com> wrote:
> form_for cannot be, uh, "internationalized" because the various flavors
> 'connected_to_country'),
>  </div>
>
> Both parameters (locale, somename) will end up in the params received by
> the relevant controller.
>
> - - -
>
> I find using raw html to be far far easier to understand and more
> convenient to use than the hidden_field api documented in
> http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html.

What is wrong with
<%= hidden_field_tag "locale", params[:locale] %>

Colin
Posted by Ralph Shnelvar (ralphshnelvar)
on 2010-03-07 15:14
Colin Law wrote:
> On 7 March 2010 11:00, Ralph Shnelvar <lists@ruby-forum.com> wrote:
>> form_for cannot be, uh, "internationalized" because the various flavors
>> 'connected_to_country'),
>> �</div>
>>
>> Both parameters (locale, somename) will end up in the params received by
>> the relevant controller.
>>
>> - - -
>>
>> I find using raw html to be far far easier to understand and more
>> convenient to use than the hidden_field api documented in
>> http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html.
> 
> What is wrong with
> <%= hidden_field_tag "locale", params[:locale] %>
> 
> Colin

I was going to argue the point, Colin, but, yeah, that works.

It's always nice to have an additional level of indirection.  So ... 
thanks again.
Posted by Marnen Laibow-Koser (marnen)
on 2010-03-09 18:42
Ralph Shnelvar wrote:
> Colin Law wrote:
>> I think maybe you cannot include query terms in form_for :url.  An
>> alternative is to use hidden fields in the form.
> 
> Colin, thank you.   You appear to be correct.
> 
> So for the next poor person fighting to get internationalization to work
> (I18n.  See http://guides.rubyonrails.org/i18n.html Section 2.3 Setting
> and Passing the Locale) ..
> 
> form_for cannot be, uh, "internationalized" because the various flavors
> of url_for that I18n hooks will have the locale parameter disappear if a
> form, any form, is used. It appears not to be an ruby/rails/I18n problem
> but an arcane "problem" in html.
> 
> Thus ... if you want to pass a couple of extra parameters when using
> form_for ... you can do what I did.  I'm pasting in my adlterated code:
> 
> - - - - - - -
> <% form_for @countryToSearchFor,
>      :url => signed_in_path(:action => "show", :id =>
> 'connected_to_country'),
>      :html => {:method => :get} do |f| %>
>   <input type="hidden" name="locale" value="<%= params[:locale] %>" >
>   <input type="hidden" name="somename" value="someothervalue" >
[...]

Why not store the locale in the session, instead of passing around 
hidden fields?

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Ralph Shnelvar (ralphshnelvar)
on 2010-03-14 00:55
Marnen Laibow-Koser wrote:
> Why not store the locale in the session, instead of passing around 
> hidden fields?

From http://guides.rubyonrails.org/i18n.html
- - - -
You may be tempted to store the chosen locale in a session or a cookie. 
Do not do so. The locale should be transparent and a part of the URL. 
This way you don’t break people’s basic assumptions about the web 
itself: if you send a URL of some page to a friend, she should see the 
same page, same content.
- - - -
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.