I'm on api.rubyonrails.org trying to read through some of these helpers to build out a form, but I keep getting errors like undefined method 'merge'. Unfortunately,
number_field(object_name, method, options = {})
Returns an input tag of type “number”.
doesn't help much at all. I've got that object_name is the name field for the generated input, but i'm fuzzy on what method, and options are used for. I just want a series of labels next to inputs, where clicking the label focuses the input, but all I can get is the first input focused regardless of the label you click.
The syntax I'm using is:
<%= f.label :item_data, i.name %> <%= f.number_field :item_data %>
i is an item from an each do. I realize this will give them all the same name, I'm just not sure what else to do about it.
The number_field is a method used to create an html input tag with type "number". Object_name is the name of the object of your form (say for instance, item). Method is the name of the actual field (for instance, cost). Options are any options you want to pass into the html field, like it being disabled, read-only, giving the tag a class/id, and so forth. If you don't pass in any options, the = {} means it just defaults to no options.
In conjunction with form_for, the syntax changes to something like this:
<%= form_for #item do |f| %>
<%= f.number_field :cost, id: "sample", class: "example" %>
<% end %>
Look at this documentation for more information on the options you can pass. Yes, it's different syntax, but the options will be the same.
Related
Background: My goal is for a view to display a list of "condition" has_many objects, which are themselves STI subclasses of a StateDescription. I want a user to be able to pick what type of state description they are from a drop down menu (which will conditionally display a different type of form, eventually)
Inside of my main forms, I am doing a nested form like this:
<%= f.fields_for :conditions do |e| %>
<li>
<%= e.select(:type, StateDescription.subclasses.collect{|x| x.to_s}, options_for_select(StateDescription.subclassSelectForms)) %>
<br>
<%= e.label :title %>
<%= e.text_field :title %>
</li>
<% end %>
This works just fine with the text field at the bottom there (I can change values and save them, etc). But when I try to do a select statement, it explodes.
Specifically, if I don't use e.select and just do:
<%= select(:type, StateDescription.subclasses.collect{|x| x.to_s}, options_for_select(StateDescription.subclassSelectForms)) %>
it renders just fine, but doesn't actually change any values as it is not associated with a model.
If I get rid of trying to have it display a value different than it submits and just do
<%= e.select(:type, StateDescription.subclasses.collect{|x| x.to_s}) %>
Then it works just fine(I can submit, the value is saved to the database, I can retrieve it, etc).
I can LEAVE it like this, but I had wanted to have my more human readable string display instead of a rails class thing (which might look like gibberish to a non-programmer end user).
So, my question is: Why do options_for_select break when nested and associated with a form, but dont' seem to when not associated with a form? Is it just a known bug? Am I doing something wrong?
Edit:
.subclasses is a standard rails calls which returns a list of subclasses for an object.
.subclassSelect forms goes through each subclass, and turns it into a hash of the form:
{subclass.to_s => subclass.human_readable_string} and compiles all of them into an array.
I know this array of hashes works, because if I do NOT put "e" on the front, it displays correctly and works "right" for what I have done (i.e. the subclass is returned correctly based on which human readable string I select from the drop down menu, but since it's not actually associated with any form, it doesn't get set in the controller).
There's this form in which the user select one of a few options. His answer will be displayed elsewhere, so it must be well formatted.
The thing is when you set the radio's value in Rails it also uses it for the id attribute, which will be invalid if I use spaces and other special characters.
See the example below:
<%= radio_button :a, "Yes, I want bananas." %>
<%= radio_button :a, "No, I rather have strawberries." %>
I would like this to be outputted as this:
<input type="radio" name="a" id="a_yes_i_want_bananas" value="Yes, I want bananas.">
<input type="radio" name="a" id="a_no_i_rather_have_strawberries" value="No, I rather have strawberries.">
I tried forcing a value option in the radio_button but it didn't work.
Any ideas ?
Update.
To make it clear, the user will only select one option. But my problem is:
The value you give when calling form.radio_button is used both as id and value. I want the id to be parameterized, but the value to keep its format - like uppercases and spaces.
I want the string "No, I rather have strawberries." to be submitted as it is, but the id to be "a_no_i_rather_have_strawberries".
Please tell me if it isn't clear yet. As you can see I'm not that good in english. =P
Another way to put that is:
I'd like to do this:
<%= radio_button :foo, "my_super_option", :value => "You are awesome! %>
But Rails ignores my value option.
You could alternatively use:
<%= radio_button_tag(:a, "bananas") %>
<%= label_tag(:a_bananas, "Yes, I want bananas.") %>
<%= radio_button_tag(:a, "strawberries") %>
<%= label_tag(:a_strawberries, "No, I rather have strawberries.") %>
I think your last code example doesn't work because the first string parameter is the value, so the value in the options hash is ignored.
Have you tried specifying the id as part of the options hash?
<%= radio_button :a, "Yes, I want bananas.", id: "yes_i_want_bananas" %>
<%= radio_button :a, "No, I rather have strawberries", id: "no_i_rather_have_strawberries" %>
I think I got crazy or something. Rails magically begun to keep the value attribute with special characters and yet parameterized the id attribute.
This:
<%= f.radio :option, 'Hi! Would you like a coffe ?'%>
Became this:
<input type="radio" name="form_option" id="form_option_hi_would_you_like_coffee" value="Hi! Would you like a coffe ?">
Now everything works as expected. Sorry to trouble you folks and thanks for everything.
This actuallly looks like the choice of an input. you should probably use checkboxes for this:
<%= f.checkbox :flavor, "strawberry" %>
<%= f.checkbox :flavor, "bananas" %>
a radio button is meant for one selection out of a several options, what your example suggests is that you need to mark a single selection.
you can always use radio buttons but give them specific values like "strawberries" and "bananas" - and use your explanatory text in the label.
In my form, I used the span tag like the following:
<%= content_tag :span, f.object.User, class: 'username' %>
It looks like the following in HTML after i selected the value:
<span class="user" style="">Antony</span>
The problem is id doesn't get the value to the database when we create a form. I don't know the exact problem is. I want to use this content tag instead of text_field to get the value.
Thanks.
When you submit an HTML form, the only values that get POSTed are those that are in input fields such as text fields, selects, checkboxes, buttons, etc. Content that is simply on the page -- in a span or not -- will not get posted back to the server. That isn't a Rails issue, it's just the way HTML works.
I'm not exactly sure what you're trying to do here, but a common approach when you want to display a value (not in an input box) and also post the value back with the form, is to both render the value on the page (in a span or however you want) and also add a hidden input field (hidden_field_tag) that also has the value in it.
Yeah, Jacob is correct. Better create a hidden field
<%= f.hidden_field :user, class: 'user' %>
<%= content_tag :span, f.object.User, class: 'username' %>
The first line get the value in it. I hope, Jacob answer would help you. :)
Ok, I've been digging around and haven't found an answer to this. I have a pretty complex custom rails form generator application that renders pages, sections, & surveys (forms) from a database.
It does validation server side (haven't finished the javascript yet and want both types), and I have it working for all types of form input objects, except for radio buttons. Because I can't get it to submit radio buttons to show up in params when they are not checked. As opposed to just looking for radio buttons outside of the params, I'd like to find a way to check them in my response loop (if possible).
I've seen this suggestion of binding it to the model to make sure it validates, but my questions are unique and therefore I don't have a model object I'm binding it to.
My form is declared in one partial:
<%= form_tag take_surveys_path, :id => "take_surveys_new", :method => :post do %>
and my code for generating the radio button (part of a partial that looks at a question type field):
<% when 'Radio Button' %>
<% question.answers.each do |answer| %>
<%= radio_button "response[#{question.id}]content", question.question_text, answer.value %>
<%=answer.value%>
<br />
<% end %>
<% end %>
I iterate over params in my "take_survey_controller" and then check each question to see if it's valid, which includes regex and required validation based on some attributes set in my question object:
params[:response].each do |question_id, answer|
#find my questions and answers, call
if item.valid?
#do a bunch of saving and stuff...
end
end
but this never gets called for radio buttons because empty radio buttons don't post to the params.
Any suggestions or help? Happy to share more code if needed.
What you're looking for is a way to set a default value for your radio buttons. Checkboxes do this by default, but in this instance it doesn't look like it's happening for your radio buttons.
Try this:
<%= hidden_field "response[#{question.id}]content", question.question_text %>
<%= radio_button "response[#{question.id}]content", question.question_text, answer.value %>
It sounds like part of your validation should include checking input params. If a radio button isn't selected it won't submit a value, thus you need to have a list of question ids and ensure each question was submitted before moving into your params response loop.
array_of_question_ids.each do |id|
handle_missing_question(id) unless params[:response][id]
end
params[:response].each ...
I've got a form_for form, and within it, I'm using a form helper check box. The model has a field that's a boolean value, but I'd like to be able to display it on my form in terms of its converse since I think that's easier for users to understand.
Is there any way for me to have a Rails form helper act as if the model's boolean field is flipped?
Example:
<% form_for :user do |form| %>
<%= form.check_box :privacy_disabled %>
<% end %>
In this example, the User model has a boolean field privacy_disabled. I would like to display this in the form as check here to enable privacy.
The checkbox helper function has the ability to set its checked and unchecked values, but inverting these does not seem to properly populate the checkbox with the pre-saved value.
This is how I ended up solving this issue, I was actually close with my first attempt:
<%= form.check_box :privacy_disabled,
{:checked => !#user.privacy_disabled},
0,
1 %>
So the values returned by the checkbox are flipped, and whether it starts out checked is manually flipped as well.
Not the most elegant solution, but you could do something like the following:
Add these methods to your user model:
def privacy_enabled=(val)
self.privacy_disabled = val == "0"
end
def privacy_enabled
!privacy_disabled
end
Change the form to:
<%= form.check_box :privacy_enabled %>
Again, not elegant, but it works.