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' %> <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&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?
I think maybe you cannot include query terms in form_for :url. An alternative is to use hidden fields in the form.
Colin
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 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| %> <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 ActionView::Helpers::FormHelper.
- - -
I find it amusing that the spam filter does not like the (proper spelling of) the word "adlterated"
What is wrong with <%= hidden_field_tag "locale", params[:locale] %>
Colin
Colin Law wrote:
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 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| %> <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 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. - - - -