Form_for, submit, and parameters disappearing

Consider the following:

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’ %>

<%= f.submit shnI18n.literal('search_for_connection') %>
<% end %> - - -

The debugger tells me that
signed_in_path(:action => “show”,
:id => ‘connected_to_country’, :something => ‘other’)

==> “/signed_in/connected_to_country?locale=en&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?

On 6 March 2010 18:50, Ralph S. [email protected] wrote:

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

On 7 March 2010 11:00, Ralph S. [email protected] wrote:

form_for cannot be, uh, “internationalized” because the various flavors
‘connected_to_country’),

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
ActionView::Helpers::FormHelper.

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

Colin

Colin L. wrote:

On 7 March 2010 11:00, Ralph S. [email protected] wrote:

form_for cannot be, uh, “internationalized” because the various flavors
‘connected_to_country’),
�

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
ActionView::Helpers::FormHelper.

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.

Colin L. 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 Rails Internationalization (I18n) API — Ruby on Rails Guides 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| %>


<%= f.select :country,
language_neutral_array_of_countries,
:prompt => shnI18n.literal(‘enter_country’) %>



<%= f.submit shnI18n.literal('search_for_connection') %>
<% end %> - - - - - - -

You will note the

The first is the critical one where the locale is being passed.
The second 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
ActionView::Helpers::FormHelper.


I find it amusing that the spam filter does not like the (proper
spelling of) the word “adlterated”

Ralph S. wrote:

Colin L. 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 Rails Internationalization (I18n) API — Ruby on Rails Guides 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| %>


[…]

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

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

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

From Rails Internationalization (I18n) API — Ruby on Rails Guides


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.