View Output
I am trying to loop through an array of values using simple_form.
Where #resume.employerorg holds the following values:
["Kaiser Santa Clara Hospital", "Kaiser Hospital",
"UC Medical Center", "UCD Medical Center", "Some Hospital", "Sutter Auburn Faith Hospital", "Kaiser Roseville Hospital",
"Sutter Roseville Hospital"]
In my view I have the following
<h3>7. Employer Names</h3>
<%= simple_form_for #resume do |f| %>
<% #resume.employerorg.each do |i| %>
<%= f.input :employerorg, label: 'First Employer' %>
<%= f.button :submit %>
<% end %>
<br/>
<% end %>
But this creates 8 input fields each filled with
Kaiser Santa Clara Hospital Kaiser Hospital UC Medical Center UCD
Medical Center Some Hospital Sutter Auburn Faith Hospital Kaiser
Roseville Hospital Sutter Roseville Hospital
How would I fill each input with one individual item from the array rather than fill each input with the whole array 8 times?
You should use the value of i to generate the input. Something like this:
<h3>7. Employer Names</h3>
<%= simple_form_for #resume do |f| %>
<% #resume.employerorg.each_with_index do |i, index| %>
<% input_id = "employerorg_#{index}".to_sym %>
<%= f.input input_id, label: 'First Employer', input_html: {value: i} %>
<%= f.button :submit %>
<% end %>
<br/>
<% end %>
You need to generate the input_id based on the item index. Otherwise, all the inputs will have the same name and ID and would be undistinguished.
<h3>7. Employer Names</h3>
<%= simple_form_for #resume do |f| %>
<% #resume.employerorg.each do |i| %>
<%= f.input :employerorg, label: 'First Employer' %>
<%= f.button :submit %>
<% end %>
<br/>
<% end %>
Understanding what's going on here: every time you tell Ruby/Rails to .each do an object Collection, you specify a "pointer", if you will, to refer to each object within, in your case, you've specified i.
If you're using a simple array as your input, you can just use i to output the value.
As Fredrico Mentioned, you should also be utilizing the object's index to generate your ID.
<h3>7. Employer Names</h3>
<%= simple_form_for #resume do |f| %>
<% #resume.employerorg.each do |i, index| %>
<%= f.input "emp-#{index}".to_sym, label: "Employer: #{index}", input_html: {value: i} %>
<% end %>
<br/>
<% end %>
Related
I have a dashboard that shows results fetched from users and nannies model.
In my user.rb, I have
User has_many :nannies
The nanny.rb has
Nanny belongs_to :user
User has some basic attributes (name, email, phone etc)
Nanny has other attributes (gender, hours, days etc)
Right now, I have these lines to implement Ransack and fetch users according to filters. The index method of the controller is
#users = User.ransack(params[:q])
#people = #users.result
I can filter out the results based on attributes from Nanny. The filter form in the view is like this
<%= search_form_for #users do |f| %>
<%= f.label :firstname_cont %>
<%= f.search_field :firstname_cont %>
<%= f.label :lastname_cont %>
<%= f.search_field :lastname_cont %>
<%= f.label :role_eq %>
<%= f.search_field :role_eq %>
<%= f.label :nannies_gender_cont %>
<%= f.search_field :nannies_gender_cont %>
<%= f.label :nannies_hours_eq %>
<%= f.search_field :nannies_hours_eq %>
<%= f.submit %>
<% end %>
The results are displayed like this
<% #people.each do |test| %>
<%= test.id %>
<%= test.firstname %>
<%= test.email %>
<%= test.hours %>
<% end %>
I am getting an error 'undefined method hours for User' (which obviously belongs to the Nanny model).
I need to be able to achieve the following
Load all Users and its associated Nannies
Run Ransack and filter out the results
Display all attributes from all associated models in the view
Would really appreciate any guidance/correction in this issue.
Assuming your question is how to fix undefined method hours for User:
The following solution is not really a Ransack "thing", but more just a Rails "thing":
<% #people.each do |test| %>
<%= test.id %>
<%= test.firstname %>
<%= test.email %>
<% test.nannies.each do |nanny| %>
<%= nanny.hours %>
<% end %>
<% end %>
You need to loop through each nanny that belongs to test (of which is a User record), and from there only when you can access hours value for each nanny
Let's say I have a cat model and a life model. And let's say a cat (#cat) has_many lives, and a cat accepts_nested_attributes for a life.
Now, if I wanted to update 7 lives (#lives) at once, using one form_for(#cat), how would that form look like? This is what I've tried, but in this form only the attributes for the last life are passed to the params hash:
<%= form_for(#cat) do |f| %>
<% #lives.each do |life| %>
<%= f.fields_for(life) do |l| %>
<%= l.input :date_of_birth, as: :date %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
You need to build the attributes in your controller
#cat = Cat.find(<criteria>)
#cat.lives.build
In your example, you have a loop inside a loop. Try this:
<%= form_for(#cat) do |f| %>
<%= f.fields_for(:lives) do |l| %>
<%= l.input :date_of_birth, as: :date %>
<% end %>
<%= f.submit %>
<% end %>
Here's the code I have so far:
<% #prayers.each do |p| %>
<%= p.date %> <%= p.time %>
<%= simple_form_for #prayers do |f| %>
<%= f.input :name %>
<% end %>
<% end %>
I've got a simple prayers table with date, time and name, all string fields. In the view I want to show the date and time, and have the user enter his name in the box he wants. (This is a signup for a 50-hour prayer vigil). This isn't a nested form, because all the fields are from the same table. The error this throws is: undefined method `to_key' for #
I'm assuming that #prayers is an array of instances of model Prayer and the form is meant for the same object from which you're printing the date and time.
simple_form_for should then refer to p instead of #prayers. The form is for a single item in the #prayers array.
<% #prayers.each do |p| %>
<%= p.date %> <%= p.time %>
<%= simple_form_for p do |f| %>
<%= f.input :name %>
<% end %>
<% end %>
I'm trying to create a basic survey app. On my take survey page I'm looping through and displaying each answer option as a radio button or checkbox as a form_for to create a user's choice. The choices are working great for the questions that are single choice (or radio buttons), but they aren't saving for multi select questions. I'm pretty sure this has to do with the form I have for the checkbox.
It seems like I should do
<%= f.check_box :answer_id, answer.id %> <%= answer.title %> <br>
similar to how I'm creating the radio button but that throws an error
undefined method `merge' for 14:Fixnum
Here's my code that displays:
<h3>Questions:</h3>
<ul><% #survey.questions.each do |question| %>
<li><p><%= question.title %></p></li>
<% choice = question.choices.build %>
<% if question.single_response == true %>
<%= form_for [question, choice] do |f| %>
<% question.answers.each do |answer| %>
<%= f.radio_button :answer_id, answer.id %> <%= answer.title %><br>
<% end %>
<%= f.hidden_field :survey_id, value: #survey.id %>
<%= f.submit %>
<% end %>
<br />
<% else %>
<%= form_for [question, choice] do |f| %>
<% question.answers.each do |answer| %>
<%= f.check_box :answer_id %> <%= answer.title %> <br>
<%= f.hidden_field :survey_id, value: #survey.id %>
<% end %>
<%= f.submit %>
<% end %>
<br />
<% end %>
<% end %>
</ul>
Any idea what I need to do to get it to save the answer_id to the choice so that it actually creates the choice?
Thanks!
This question is a few years old but I think it deserves a better answer. Since you are using form_for (a model backed form), then you probably want to use the form_for check_box method that you originally tried to use. In your case, it would look like this:
<%= f.check_box :choice, { :multiple => true }, answer.id, false %>
Here is the doc on this.
For checkboxes, you actually want to return an array as the parameter. There is a little funny syntax to this because we don't actually want to use the form builder methods. It should look something like this (adapt to your specific model names and methods)
<%= check_box_tag 'choice[answer_ids][]', answer.id %>
Using this syntax should tell Rails to compile all of the checked checkbox values into an array.
This Railscast goes over the topic.
i would like to use an additional collection for a fields_for. this collection should hold all the possibilities to be used in fields_for.
Lets say I have a person with tasks that will happen regularly each week on the same day. In the person form, i should have an entry for each day, even if there are not yet any saved tasks. I tried:
<% form_for(#person) do |f| %>
...
<% f.fields_for :tasks, #weekdays do |task_fields| %>
<%= weekday.name %>:
<%= project_fields.text_field :name %>
<% end %>
<% end %>
now there should be for each weekday a text field to enter the name of the task of that day. for example weekday.name = "monday" and task.name = "drinking coffee", task.weekday_id = 1
You are not iterating through the week_days. You should do like this:
<% #weekdays.each_with_index do |weekday, i| %>
<% f.fields_for :tasks do |task_fields| %>
<%= weekday.name %>:
<%= task_fields.text_field :name %>
<%= task_fields.hidden_field :weekday_id, :value => (i + 1) %>
<% end %>
<% end %>
If you have a table 'weekdays', then hidden_field value should be weekday.id
Edit: July 30
I think I completely messed up this answer. Let me try to improve it.
<% f.fields_for :tasks, #weekdays do |task_fields| %>
<%= weekday = task_fields.object %>
<%= weekday.name %>:
<%= task_fields.text_field :name %>
<%= task_fields.hidden_field :weekday_id, :value => weekday.id %>
<% end %>