From the database I am loading data, which are displayed in the form (form_for) as the radio inputs.
Support Rails any way, how to always check the first radio input in the group?
Add this to the first radio_button:
:checked => true
Although if you are using it from a model I'd highly recommend simple form
Related
I'm working on an RoR project with the Simple_Form gem. For those who don't know, simple form creates easier form helpers for use in views. My question is regarding the f.association helper which allows users to easily select associations on models via a select box, radio buttons or checkboxes.
f.association has an option called value_method which allows you to pass a method for generating the name of the individual models in the association to choose from. The default is just to_s, so it will work with select boxes. However, I'm using radio buttons, and I'd like to know if anyone knows of a way to use the render partial method as the value_method.
In my setup, I have a User model with a user.haml partial that I'd like to be rendered next to each checkbox, that way users selecting other users can have any easy to recognize user div with picture, name, and last login to choose.
Thanks, steakchaser answered my question with his comment. This is the solution:
f.association :some_association, label_method: lambda { |obj| render obj }
I realized label method is better than value method, because even with checkboxes and radio buttons, simple form still put the values in the options value="..." attribute. This caused an html rendering error.
I have a query to ask. Im using radio_button_tag with rails 3.1. The issue that I come accross is that for eg : If I have ascending and descending radio buttons, only one should be selected. But in my case both the radio button tags are getting selected.
Kindly help me.
This is a guess at best, because you haven't put your code, but:
You need to ensure that your radio buttons have the same HTML name attribute by giving your tags the same first parameter in radio_button_tag. See the documentation.
if two radio fields has same name, one can be selected at a time!
Pass true to the tag you want to be selected initially. For example, if you want 'Descending' to be checked initially, write like:
radio_button_tag 'order', 'asc'
radio_button_tag 'order', 'desc', true
I'm going to make form that:
Shows a message (text, object description) to rhe user
Shows a list of buttons. like 'comment', 'reroute' and 'close'.
3.1 If the user clicks on 'comment', shows text_area for input. The input is not for an attribute from a model, it must be later appended to the 'log' field.
3.2 If the user clicks on 'reroute', shows collection_select to choose to whom to reroute.
3.3 If the user clicks on 'close', shows text_area for input. The input must be later saved to the 'solution' field of the model.
What is the rails way to do this? Should I start learn the JQuery or there some gems instead of it? Or maybe I don't need nothind special and such a thing can be done with Rails alone?
I have a partial which includes a form, i have two different instances where i want to use this form, one if for editing and another one if for viewing only, i want to disable all of the fields in a second case. Is there any quick way to do it in rails? I know i can do it with jquery but i prefer to do it in rails.
You can wrap all elements in with_options :disabled => 'disabled' do method.
I want to create an extra radio button with some choices inside an existing rails form (I use simple_form gem). The problem is that I do not want this to point in any field in my model. I want it to be an extra field that I would like to pass.
All examples that I found introduces a hidden tag. But I do not want it to be hidden I want a radio button set.
Is that possible?
Try using radio_button_tag inside the modal form, I have used it in the past. An example would be:
radio_button_tag 'send_me_updates', 'radio', false
When you submit the form, I think you will be able to access the radio button value with params[:send_me_updates]. Should work, please try it and let me know.