Using render as the value_method in Rails Simple Form? - ruby-on-rails

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.

Related

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");
})

What it the rails way to make dynamic forms? (Is the JQuery the only option?)

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?

Extra item in a rails form

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.

What is the best way to set up a form to get a temp var?

I am making a photo upload form. Before they begin, they have two choices. They can create a new gallery, OR they can choose from an existing gallery.
I was thinking the best way to lay this out would be two have two forms. One for the create one that would take them to the create page. That's easy, and practically done.
But the second form, I need to grab the gallery name in a select box and apply it to an add_photos_to_gallery form - which is tricky for me. Do I?
a. Make a temp variable for this form and pass it to the controller, and then generate the form from what they choose? If so, how could I do that?
b. Use jquery and somehow manipulate the DOM to create a form? I got some ideas..
c. Realize I'm too dumb to know something way better and more obvious?
roughly speaking the way I would approach this is with one form,
to populate the select tag, i am assuming you create a named scope or association in your user model, eg
has_many :galleries
and populate the select tag with it
<%= select_tag "add_photos_to_gallery", #user.galleries, :include_blank => true %>
and if the params[:add_photos_to_gallery] is blank, create a new album otherwise add it to the selected gallery.
Or you could include a checkbox to toggle on or off the select tag based on if they want to use an existing gallery or not.

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