What's the Rails Way to pass data to controller method? - ruby-on-rails

After getting some data from an API, I filtered the results and showed them on a view to et the user select which data he wants to save to the database. The user doesn't have to fill anything once all the data came from the API.
My question is: what's the best way/rails way to pass all this data to the controller?
Right now I'm using a form with hidden fields. It works but I was wondering if that's the best way.
Here's how my 'form_for' looks like:
<%= form_for (#new_photoset) do |f| %>
<%= f.hidden_field :id, :value => not_imported_set.id %>
<%= f.hidden_field :name, :value => not_imported_set.name %>
<%= f.hidden_field :thumb_url, :value => not_imported_set.url) %>
<%= f.submit "Import" %>
<% end %>

I believe you're on the right track, this is way you would normally do this in rails.

Related

Creating a variable via the form rails

Value the variables written in variables #first_city, #last_city, #date_trip of the controller. Without creating an object in the database. Then pass the values of these variables to another page. It is possible to do that or not?
<%= form_for(#orders) do |f| %>
<p>Выберите маршрут</p>
<%= f.select(:first_city, #city_select, :value => :first_city, prompt: "Откуда") %>
<%= f.select(:last_city, #city_select, prompt: "Куда") %>
<%= f.text_field :date_trip %><br>
<%= f.submit "Дальше", class: "btn" %>
<% end %>
Yes.
However if you state
form_for(#orders)
it will automatically assume you are creating an Order.
However you can do
form_for #order, :url => {:action => "action"}
create your own action and access those values trough
order_params[]
And you can do whatever you want with that data
Hope i helped

Rails - How to have a form save the data from multiple inputs as a hash

I'm building a page that contains a html table, where the user can choose the name in the table header and the content in the table row.
The user will enter the information to a form, which will be parsed into a hash and saved into the DB.
Here is an example where :key will be a key of the hash (table header text) and :value will be a value in the hash (table column content).
<%= form_for([#page]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :key1 %>
<%= f.text_field :value1 %>
<%= f.text_field :key2 %>
<%= f.text_field :value2 %>
<%= f.text_field :key3 %>
<%= f.text_field :value3 %>
<%= f.text_field :key4 %>
<%= f.text_field :value4 %>
<%= f.submit "Save" %>
<% end %>
I could make a new DB table with a has_many association to the page DB and have the info stored in that table. Then that data can be rebuilt into a hash when I need to render the view.
But if I could save the entire hash into a column in the page DB, then it would save me from having to rebuild the hash every time someone visits the page.
I'm struggling to come up with how to do this in form_for.
The first issue is that it requires key and value columns in the page table. A simple fix would be change all inputs into <%= f.text_field :html_table %> (for html_table column in the page table). But then it won't be able to distinguish between a key and a value in the hash. And I also don't have any way of telling it to put the info together into a hash that is then stored into the page table's html_table column.
Not sure if this is even a possible thing but it doesn't hurt to ask before I go ahead with building the has_many association table and setting it up that way.
I'm rather confused about your question.
Can't you build a hash like this in your controller action?
html_table = {
params[:key1] => params[:value1],
params[:key2] => params[:value2],
params[:key3] => params[:value3],
params[:key4] => params[:value4]
}
Then save it to #page.html_table attribute.
EDIT:
In this case, the form is not about attirbutes of a particular object. So instead of using form_for (Link), you should be using form_tag (Link).
Example:
<%= form_tag "/pages_controller/build_html_table" do %>
<%= text_field_tag "key1" %>
<%= text_field_tag "value1" %>
<%= submit_tag "Save" %>
<% end %>

Get data attribute value from the Rails form builder without using input field

I have what I hope to be a simple question. I need to display the value for an attribute on the Edit page, while keeping the input field for the same attribute. How might this be accomplished?
Well generally you can just use the original object, like you'll have an #foo that you'll have used in your form_for statement, so you can just use that directly: = #foo.the_attribute
If you're within a partial, or elsewhere where you have only the form builder instance, then you can refer to the underlying object with the .object method, eg.:
= form_for #foo do |f|
# in here, f.object == #foo
In my case, I'm working with accepts_nested_attributes_for in two models. Event accept nested objects from Speaker. And Speaker has a perfil_id attribute which could be ['Maker', 'Developer', 'Entrepreneur', ...].
The Speaker's form is a partial rendered from the principal form, Event's form:
<%= form_for(event) do |f| %>
<%= f.text_field :title %>
<%= f.label :title, 'Event name' %>
<%= f.fields_for :speakers do |builder| %>
<%= render 'events/partials/speaker_fields', f: builder %>
<% end %>
<%= f.submit %>
<% end %>
Partial
<%= builder.number_field :name %>
<%= builder.label :name %>
<% options = options_from_collection_for_select(#profiles, 'id', 'name', f.object.member_profile_id ) %>
<%= select_tag "event[speakers_attributes][profile_id]", options, prompt: 'Select a Profile' %>
When editing Event's Speakers I wanted a select_tag to select the profile name for the actual Speaker.
I could not use an input field for this. So I need to get the correct values from the builder object and I get what I need by doing this:
f.object.profile_id
Passing it as a fourth param to the select options I get this working:
<% options = options_from_collection_for_select(#profiles, 'id', 'name', f.object.profile_id ) %>
I hope it could be useful for you too!

Rails3: How to implement multiple checkboxes in a form?

What I have now gives me a dropdown menu where I can only select one:
<%= form_for(#submission) do |f| %>
<%= f.collection_select :id, Submission::SUB_ID, :to_s, :to_s %>
<% end %>
where SUB_ID=[1,2,3] in model Submission
I want to implement a checkbox instead of a dropdown menu so that I can select multiple SUB_ID (i.e. 1&2 or 1&3 or 2&3 or 1&2&3). I tried to use this but it does not work:
<%= f.check_box :id, Submission::SUB_ID, :to_s, :to_s %>
Any idea?
Try this:
# view
<%= form_for(#submission) do |f| %>
<%= Submission::SUB_ID.each do |sub_id| %>
<%= f.checkbox 'ids[]', value: sub_id, checked: #submission.id == sub_id %>
<%= sub_id %>
<% end %>
<% end %>
# controller
params[:submission][:ids].each do |checked_sub_id|
# do your logic here
end
you have to iterate over SUB_ID
somehow like this...
<% Submission::SUB_ID.each do |ssid| %>
<%= f.check_box "ids[]", value: ssid %>
<% end %>
or you can use formtastic gem. it has :as=>:check_boxes input fields http://www.ruby-doc.org/gems/docs/n/nuatt-formtastic-0.2.3/Formtastic/Inputs/CheckBoxesInput.html
The core answer is you need to loop over each item in Submission::SUB_ID and make a checkbox for each id. Depending on how your models are set up and what you want to do - you may need to be much more involved in the form building. I hesitate to provide specific examples without know more about how you want the data to come back to the controller
<%= form_for(#submission) do |f| %>
<% Submission::SUB_ID.each do sub_id %>
<%= f.check_box_tag 'submission_ids[]', sub_id %>
<% end %>
<% end %>
Note that that will not default anything to checked and it does not come back as part of the submission parameters.
Usually when I have a similar situation I'm using nested forms to add or remove objects.
If you're using Rails 4, there is a new helper, collection_check_boxes, which helps streamline the building of your check boxes.
<%= f.collection_check_boxes :submission_ids, Submission::SUB_ID, :to_s, :to_s %>
Documentation:
Form builder version - which wraps...
...the general form options helper
If you look at the documentation in the second link, you'll also find how to use the optional block syntax to customise the HTML structure for each check box.

Forms blank when rendering a partial when using a collection of objects. Help!

Alright, I know my title is a little obscure but it best describes the problem I am having.
Essentially, I have a list of users, and want to be able to edit their information in-line using AJAX.
Since the users are showing up in rows, I am using a partial to render the data and the forms (which will be hidden initially by the ajax), however, when the rows are rendered currently only the last item has it's form's fields populated.
I suspect this has something to do with the fact that all the form fields have the same id's and it is confusing the DOM. But I don't know how to make sure the id's are unique.
Here is a small example:
In my view:
<%= render :partial => 'shared/user', :collection => #users %>
My partial (broke down to just the form) note that I am using the local variable "user"
<% form_for user, :html => {:multipart => true} do |f| -%>
<%= f.label :name, "Name*" %>
<%= f.text_field :title, :class => "input" %>
<%= f.label :Address, "Address" %>
<%= f.text_field :address, :class => "input" %>
<%= f.label :description, "Description*" %>
<%= f.text_area :description, :class => "input" %>
<% end -%>
When the html is rendered each form has a unique id (for the id of the user) but the elements themselves all have the same id, and only the last user form is actually getting populated with values.
Does anyone have any ideas?? :)
Thanks in advance!
Alright, after having some lunch and regaining some brain cells, (and with a little help from Google) I figured this one out.
When passing a collection to a partial like this:
<%= render :partial => 'shared/user', :collection => #users %>
Rails creates a counter variable that you can use to define an index for the form in the form of "variable_counter":
<% form_for user, :index => user_counter, :html => {:multipart => true} do |f| -%>
This adds the index number to the form id as well as all the field id's and solved my little problem. :)
I hope this helps out someone else with this issue. :)

Resources