I'm making an input form whose features don't fit so nicely into the general form_for template rails provides. I figured I would write my own HTML mimicking the html output of form_for, but embedded with other form_for elements.
For fields I could use the rails framework for I did. For the others I made hidden fields to store what was going to Rails, and regular input fields whose values I manipulated with JavaScript to put into the hidden fields.
This is one such field:
State:<br>
<input type="text" class = "state name_input administrative_area_level_1">
<div class="field">
<input type="hidden" name="address[state]" id="state">
</div>
When I send the value of the hidden field to the console, I get a good response:
state 37
Which means the state field holds the value 37. Perfect.
Except that when I try to make my model, the params come in empty:
"state"=>"",
I do have one field that works that isn't hidden:
Street:<br><input type="text" id="street" name="address[street]">
So I changed the state input type to number, which is what it would be if it weren't hidden, but same result. Why can't rails find my param?
You can show post more detail. I can't understand 100% things you do because you write pretty simple. But i guess that you need test params name is address[state] but it's not only state becase if only state of course it nil.
if html of you rendered same up then i suggest you should put one debug in controller to see params return controller when submit form.
For example: in controller you add line:
logger.debug params
to see right params that form send.
If your form is not tied directly to a object model, it might be difficult to use the form_for helper. In those cases you should use form_tag helper. Read more about form_tag helper here.
Refer this stackoverflow answer for the difference between form_for and form_tag helpers.
Hope this helps.
Related
I'm using simple form and I have a long form divided in partials, each partial represents a category.
For example, identification_form is:
I'd like the inputs to get a name like this:
Is possible to do this?
I can change the names by myself, but the problem is simple_form is not able to create the names for the stat_date and end_date (act[start_date(1i), act[start_date(2i), act[start_date(3i), it adds the same name for the three inputs.
Thanks!
Yes it is!
f.fields_for :identification do |identification_form|
identification_form.input :name
The above will give you:
<input type="text" name="act[identification][name]" />
This should then work with dates.
Edit:
I think you should be using date_select rather than input.
What would be a better "Rails way" of doing it
- shops.map(&:id).each do |id|
<input id="p_shop_ids_#{id}" name="p[shop_ids][]" type="hidden" value="#{id}" />
I recently read that in Haml is in this sense downwards compatible. But it feels like it should be done with the rails checkbox helper instead
You can use the hidden field helper.
Based on #Sontya's comment (thanks!!)
- shops.map(&:id).each do |id|
= hidden_field_tag "p[shop_ids][]", id, id: "p_shop_ids_#{id}"
produces the correct output (I had to add the id-option)
Here's my examples that produce the same result :
# `enterprise_registration` is an already created/saved object
form_for enterprise_registration, method: :put do |format|; format; end
form_for enterprise_registration, url: logo_url, method: :put do |format|; format; end
form_for enterprise_registration, url: logo_url, html: {method: :put} do |format|; format; end
This returns the form with the method attribute set to POST.
Why is that happening, do you think? And how can I make it a :put request?
Update
I now understand that Rails forms embed a hidden _method and set it to put, but my form is still getting delivered as a POST that is preventing my form from finding my matching PUT URL
form tag has only GET or POST method allowed. See also here for more explanation. Rails, however, has his own method to handle GET/POST/PUT/PATCH requests. If you would examine any of your form defined as either form_for or form_tag in Rails, you will notice that first element of the form is a hidden <div> which contain two hidden fields:
<div style="display:none">
<input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="patch">
<input name="authenticity_token" type="hidden" value="9i5eRhwhx4NhvSxqIJm6cv9x6NSlY82hpNpfrpk/I0c=">
</div>
First field called _method contains the form action which is the request type for controller.
Web browsers are actually only programmed to receive POST and GET requests (I'm not sure why). The way Rails mimics full REST (which includes put and delete) is include those in hidden fields. So technically it's sending a POST, but with the PUT attached sort of awkwardly in that hidden field.
I'm assuming that Rails is still doing that sort of conversion behind the scenes still.
I have a checkbox
<%= f.check_box :anonymous %>
And my table has a column anonymous which is true or false.
Code generated in html:
<input name="comment[anonymous]" type="hidden" value="0" />
<input id="comment_anonymous" name="comment[anonymous]" type="checkbox" value="1" />
Now, for some reason when I add data it's not saving if my anonymous checkbox is checked or not.. it's not changing data in database.. All other fields gets saved except anonymous.
What can be the problem ?
Use #check_box_tag instead:
<%= check_box_tag(:anonymous) %>
From the official guides:
Array parameters do not play well with the check_box helper. According
to the HTML specification unchecked checkboxes submit no value.
However it is often convenient for a checkbox to always submit a
value. The check_box helper fakes this by creating an auxiliary hidden
input with the same name. If the checkbox is unchecked only the hidden
input is submitted and if it is checked then both are submitted but
the value submitted by the checkbox takes precedence. When working
with array parameters this duplicate submission will confuse Rails
since duplicate input names are how it decides when to start a new
array element. It is preferable to either use check_box_tag or to use
hashes instead of arrays.
My model has a property of "output" and my form has a TextBox named "output", both spelled exactly the same. When I fill out the form and post it, the returning view has unexpected results.
Specifically, I receive the posted "output" form variable which is then bound to my Model in the Controller Method, then I change this variable to a different value before I pass the Model back to the view.
the posted output property is "one thing"
my Model property is set to "another"
I have this in my code: <%= Html.TextBox("output") %>
Which renders to this: <input id="output" name="output" type="text" value="one thing" />
However, when debugging, the Model does contain the correct value ("another").
Its using the posted value rather than the value assigned in the controller. Any ideas why and how to fix it?
Assuming you're using a strongly typed view like so:
<%# Page ... Inherits="System.Web.Mvc.ViewPage<...TestModel>" %>
And having an EditorFor replacing your
<%= Html.TextBox("output") %> like so <%= Html.EditorFor(p=>p.output) %>
you can do in your Controller
ModelState.SetModelValue("output", new ValueProviderResult("Some string", string.Empty, new CultureInfo("en-US")));
A similar question has been asked here