If have a RoR application that I upgraded from 2 to 3.2, and everything eventually got fixed, but I have some strange behavior from a form_tag.
The form code is:
<%= form_tag '/public/checkem' do %>
<%= hidden_field "vals", value = picks.draw %>
<%= hidden_field "val_index", value = xcount %>
<%= submit_tag picks.draw_date %>
<% end %>
Where picks.draw is an array which, when executed in turn generates:
<form accept-charset="UTF-8" action="/public/checkem" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="ET8OgURJpwvnQ+18lK1xKaFXTBLMuMXVw4AoM/gVEYw=" /></div>
<input id="vals_6,16,45,54,60,15" name="vals[6,16,45,54,60,15]" type="hidden" />
<input id="val_index_5" name="val_index[5]" type="hidden" />
<input name="commit" type="submit" value="10/25/2013" />
</form>
The idea being that the hidden input (vals) contains an array of numbers. This value is then processed correctly as an array in the /public/checkem function, but when it gets re-rendered here:
<span class="elem" style="background-color: #b0b040; color: #000000;"><%=#xpicks[0]%></span>
where #xpicks[0] is the first element of the array
<span class="elem" style="background-color: #b0b040; color: #000000;">{"2</span>
It's picked up what looks like the start of a hash. When I look in the log file I see this line, which confirms that the array was changed to a hash when the form was submitted:
Processing by PublicController#checkem as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"ET8OgURJpwvnQ+18lK1xKaFXTBLMuMXVw4AoM/gVEYw=", "vals"=>{"2,11,42,64,74,2"=>""}, "val_index"=>{"1"=>""}, "commit"=>"11/05/2013"}
This all worked fine in version 2, but I cannot see where, or why the array gets passed as a hash.
Apparently the use of "hidden_field" is indicated when a model and "form_for" is used. If there is no model you should use "hidden_field_tag"
Related
Submitting a basic xhr-form in a turbolinks-activated rails project (5.1), does trigger the correct server-side response but the client (turbolinks) discards the response. Neither get nor post do work. Tested in Firefox and Chromium. Forms with redirection work as intended.
Here's a simple test page that fails for me
test.html.erb:
<h1><%= #value %></h1>
<%= form_with url: 'test' do |f| %>
<%= f.check_box :anything %>
<%= f.submit %>
<% end %>
controller:
def test
if request.post?
#value = 'post'
else
#value = 'get'
end
end
routes:
post '/test', to: 'controller#test'
get '/test', to: 'controller#test'
rails response payload:
<h1>post</h1>
<form action="test" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="X1sh+tc/Jx4P0rdDqc2CD1RetKQKr+lVh4fD29JOMY/b+PdPNgw1qyzoyv6t3I2I+6jV1mmn6voXN+YySaUISw==" />
<input name="anything" type="hidden" value="0" /><input type="checkbox" value="1" name="anything" />
<input type="submit" name="commit" value="submit" data-disable-with="submit" />
</form>
</div>
The form loads like expected, but after submitting the page does not change. Inside the xhr-response the correct answer is visible, but it does not get applied to the DOM.
Is this a bug, or am I using turbolinks wrong?
I'm seeing this as well and I'm not sure how to proceed. What is nice about Turbolinks is that you don't have to create js responses for each request. This is true until you submit a form. At that point, you have no choice but to create a js.erb respone file or change the form to local: true. Am I missing something?
Try using data: { turbolinks: false } in the any links to the page containing the form.
E.g.
<%= link_to "Get in Touch", 'contact', data: { turbolinks: false } %>
I have a text field that stores page content created with a WYSIWYG HTML Editor.
Text field (content):
<form accept-charset="UTF-8" action="/myaction" method="post">
<input name="myaction[product]" type="hidden" value="Other" />
<input name="myaction[product]" type="hidden" value="Car" />
<input class="btn btn-primary" type="submit" value="Get a Car"/>
</form>
Show page
<% highlight(raw(#page.content),params[:search]) %>
The form does not seem to display or show the bootstrap form and button. It seems to be getting removed. What can I do to get the form to display?
With Form_for:
- #questions.each do |secret_question|
= f.check_box :question, {multiple: true}, secret_question.id, 0
= f.label :question, secret_question.body
While saving, if I've checked last element - in base saved right value,
but if I've checked others elements (from first till penultimate) - in base saved '0'
What is wrong?
Thank you
Update1:
in controller get params:
private
def contact_params
params.require(:contact).permit(:name,... :secret_question)
end
Update2:
Result form:
<form id="new_contact" class="new_contact" method="post" accept-charset="UTF-8" action="/contacts" enctype="multipart/form-data"
<input type="hidden" value="0" name="contact[secret_question][]"></input>
<input id="secret_question_1" type="checkbox" name="contact[secret_question][]" value="1"></input>
<label for="secret_question_1">1</label>
<input type="hidden" value="0" name="contact[secret_question][]"></input>
<input id="secret_question_2" type="checkbox" name="contact[secret_question][]" value="2"></input>
<label for="secret_question_2">2</label>
<input type="hidden" value="0" name="contact[secret_question][]"></input>
<input id="secret_question_3" type="checkbox" name="contact[secret_question][]" value="3"></input>
<label for="secret_question_3">3</label>
<input type="hidden" value="0" name="contact[secret_question][]"></input>
<input id="secret_question_4" type="checkbox" name="contact[secret_question][]" value="4"></input>
<label for="secret_question_4">4</label>
Change your
def contact_params
params.require(:contact).permit(:name,... :secret_question)
end
to
def contact_params
params.require(:contact).permit(:name,... :secret_question => [])
end
to declare that parameter should be array.
Your input elements have the same name attribute: contact[secret_question][]. When submitting the form the browser sends all input elements in the order they are found inside the DOM, but Rails takes the last key-value pair it receives, if there are keys with the same name, and saves this value. This behavior conflicts with the way Rails treats checkboxes.
You can either use the option include_hidden: false for every check_box except of the first or you can use the check_box_tag instead of check_box. check_box_tag won't generate the hidden checkboxes which are causing the trouble.
I am working on the front-end part of a Rails 3.1 application. We are using a Twitter Bootstrap as CSS Framework, Devise as the authentication manager and the I18n gem for localization.
This is the devise syntax for a checkbox with its label
<%= f.label :remember_me %>
<%= f.check_box :remember_me %>
And this of course is the generated HTML
<label for="user_remember_me">Ricordati di me</label>
<input name="user[remember_me]" type="hidden" value="0">
<input id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
Since Bootstrap adds this rule for the labels display: block, the label and the checkbox are not in the same line. To have the on the same line I need an HTML output like this
<label for="user_remember_me">
Ricordati di me
<input name="user[remember_me]" type="hidden" value="0">
<input id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
</label>
As shown in the forms markup documentation
You will have noticed that the label text is in Italian, the team member who provided localization for Devised worked hard to find out how to do so, and I do not want to force he to work on it again introducing new localized strings.
I am aware of the nice fact that the FormBuidler label method accepts a block as an argument so I could do
<% f.label :remeber_me do %>
<%= f.check_box :remember_me %>
<% end %>
But this produce an HTML output whitout the label! o.O
To be specific this is what I get:
<input name="user[remember_me]" type="hidden" value="0">
<input id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
I tried to look in the source code, the f.label method calls the label method, but I can only see that if there is a block no text will be printed and that the label and block will be rendered by the label_tag of the template_object.
Before diving into a source code digging, and a no sleep night, I decided to wait a moment and ask the lifesafer community of StackOverflow for help.
Am I missing something? I am calling f.label with block in the wrong way? Is some parameter missing?
Thanks!!
I don't know if you ever got this working...
Running on Rails 3.2.6 this is what I had to do:
<%= f.label :remember_me do %>
<%= f.check_box :remember_me %>
Remember Me?
<% end %>
I hope that helps you since I came here looking for an answer to the exact same problem and couldn't find it, except through trial and error.
If you want the label and checkbox on the same line, you must include .form-inline in your
<form class="form-inline">
<label for="user_remember_me">Ricordati di me</label>
<input name="user[remember_me]" type="hidden" value="0">
<input id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
</form>
http://jsfiddle.net/baptme/66TJY/
<%= form_tag( :class => "form-inline") %>
I am trying to create a form that serves as confirmation for the destroy method on a controller. In my routes I have:
resources :leagues do
get 'delete', :on => :member
end
In my delete.html.erb, I have the following for:
<% form_for current_league, :html => {:method => :delete} do |form| %>
<%= form.submit 'Yes' %>
<%= form.submit 'No', :name => 'cancel' %>
<% end %>
current_league is a helper function defined by:
def current_league
#current_league ||= League.find_by_id(params[:id])
end
So the problem is that the form that is generated only edits the league model, as seen by the form method="post".
<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="delete" />
<input name="authenticity_token" type="hidden" value="abcdefg" />
</div>
<input id="league_submit" name="commit" type="submit" value="Yes" />
<input id="league_submit" name="cancel" type="submit" value="No" />
</form>
How can I fix this?
I think that will already work. As in rails guide http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark
However, most browsers don’t support methods other than “GET” and “POST” when it comes to submitting forms.
Rails works`around this issue by emulating other methods over POST with a hidden input named "_method", which is set to reflect the desired method:
as you see in the output form there is
<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
....
<input name="_method" type="hidden" value="delete" />
....
</form>
When reading this variable, rails understood that this is a DELETE method, not a POST method. even though the form it self is a POST.