Extra item in a rails form - ruby-on-rails

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.

Related

Conditional show/hide fields based on show/hide links

What I try to do is very simple.
I have some fields in a form that are optional to fill. To make the form look cleaner, I'd like to show simple links as Add field and display the optional fields below via js unobstructedly.
If the field is present, it will be saved into the database. The field would also have a delete link. When this delete link is clicked, the field will be removed and also its value from the database when saved.
Seems pretty simple, yet I haven't found a simple approach to this. The whole solution involves many aspects such as passing hidden data to the controller to see if the record was hidden in order to delete its data from the database.
I would prefer a gem already optimized for this purpose but I couldn't found any. What I found is intended for associated records but this is not the case. These are not associations, just optional attributes from the same model.
nested_form_fields gem its also not suitable since it needs a checkbox or radio button to be triggered and not Add field/Remove field links which I believe look cleaner.
Is there such thing as a gem to accomplish this simple thing?
Instead of trying to send hidden data etc. Why don't you just load all the form fields but display:none. When a user clicks the 'show' button you display the field. If the click the hide button, just set the field to null and hide it.
When your form reaches the controller it will save the null value to the db for the hidden fields (same as if you deleted the value).

Rails dropdown menu to select object for text_field

I am building a rails form and have an interesting problem I am trying to solve. I can't seem to find anything online to point me in the right direction. Thank you.
Is it possible to use a dropdown menu to select the :object_name for a text field?
In my head, I am picturing a collection_select form helper nested within a text_field form helper, though not sure this is possible.
In the form, I'd like the user to select the proper :object_name from an array
[:object_1, :object_2, :object_3, :object_4]
then give that entry a value with the text field
text_field(object_name, method, options = {})
The objects are all db columns in the same model.
Yes you can do that using jquery.
On change of the object names dropdown value, change the name attribute of the text field.
$('#selectObjectName').change(function(){
var field = document.getElementById("id-of-the-text-field-to-be-changed");
field.setAttribute("name", "value-came-from-the-selected-dropdown");
})

Using render as the value_method in Rails Simple Form?

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.

Rails - how to check first radio input from the list?

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

How to create one form with many possible actions in Rails?

I want to create one form with 2 buttons in rails. Both forms operate on the same data in different ways, and I would prefer to keep the associated functionality in two different methods. Previously I've redirected to the appropriate method after doing a string comparision on params[:commit], but my gut says there's a better approach. Suggestions?
Two different submit buttons that send the form to two different actions:
<%= submit_tag('Insert', :onclick=>"document.myForm.action = 'insert';") %>
<%= submit_tag('Update', :onclick=>"document.myForm.action = 'update';") %>
Instead of "myForm" you need to put whatever is in the "name" property of your tag.
You can set that property in your default form:for tag like this:
<%= form_for(#something, :html => {:name => "myForm"}) do |f| %>
Without using JavaScript, your only solution is what you mention: checking which button was clicked by looking at the POST data in the controller. This is simply due to the nature of the HTML form element. It cannot have more than one value for its action attribute.
If you're not worried about what will happen when JavaScript isn't available, then you can write some script to change the action attribute when one of the submit buttons is clicked, prior to actually submitting the form. In the case of an ajax request, it could simply submit to the correct URL directly without altering attributes on the form.
I also used the params[:commit] method on a form already. Using the I18n helpers makes this a bit less fragile as you can use the same lookup in the view and controller, so you don't encounter the problem that the string changes a bit.
Besides that I can only think of using JavaScript to handle the clicks on the buttons and then send the form data to different Rails actions (Maybe you can change the HTML action attribute of the form with JavaScript before you submit the form).
If you're using prototype.js, you can use Form.serialize() to grab your data from your form and from there use the different buttons to post to different actions.

Resources