I have two models: QuestionnaireResult and QuestionnaireOption.
The options are dynamic.
QuestionnaireResult has two columns: date_submitted and results. I want the results column to be some sort of array of the QuestionnaireOption and their value...
i.e.
option_id / value
1 / 50
2 / false
3 / true
I submit data using this form, however it's not complete and not working because I don't know what name to give the text_fields (undefined method 'not_sure_what_to_name_this' for #<Admin::QuestionnaireResult:0x4a5ef9>):
<%= form_for(#questionnaire_result) do |f| %>
<% if #questionnaire_result.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#questionnaire_result.errors.count, "error") %> prohibited this questionnaire_result from being saved:</h2>
<ul>
<% #questionnaire_result.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% #questionnaire_options.each do |questionnaire_option| %>
<% if questionnaire_option.field_type == 'Textbox' %>
<div class="field">
<%= f.label questionnaire_option.option %><br />
<%= f.text_field :not_sure_what_to_name_this %>
</div>
<% elsif questionnaire_option.field_type == 'Checkbox' %>
<div class="field">
<%= f.label questionnaire_option.option %><br />
<%= f.check_box :not_sure_what_to_name_this %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
What name do I give text_field and how do I go about saving the results and storing them in a column as an array? Or is there better ways of going about this?
Check out this reference, where it says:
2.2 Binding a Form to an Object
While this is an increase in comfort it is far from perfect. If Person
has many attributes to edit then we would be repeating the name of the
edited object many times. What we want to do is somehow bind a form to
a model object, which is exactly what form_for does.
It goes on to show examples and elaborate on the discussion. So, the fields you use in the form_for are that model's fields. To show an example, in:
<%= form_for Feed.new, id: "feed_add" do |f| %>
<%= f.submit "Add Feed", class: 'formlabel' %>
<%= f.text_field :feed_url, class: 'forminput', :autocomplete => :off %>
<% end %>
the model is Feed and the text_field is :feed_url, so this updates Feed.feed_url. It is returned to the controller in params[:feed]['feed_url'].
If you can show your model, I can advise further.
To follow up on the other part of your question, "how do I go about saving the results and storing them in a column as an array? Or is there better ways of going about this?", that's a bit different.
If what you want to do is build an array, you might want to use form_tag instead of form_for. form_for is specifically for models. form_tag is a more generalized interface for objects not necessarily models. You can see that in the same reference. To show an example:
<%= form_tag feeds_path, method: 'get', id: "feed_search" do %>
<%= submit_tag " Search ", feed_url: nil, class: 'formlabel' %>
<%= text_field_tag :search, params[:search], class: 'forminput', :autocomplete => :off %>
<% end %>
Here, the value of :search is returned in params[:search].
Regarding how to do it, you should return this information to your controller where it can process it. Views are for display. The controller can easily build and process the array so that it can be used in its decisions and/or routing.
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
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 am relatively new to rails - therefore the question should be very easy to answer. I tried to find a solution for the problem in other posts, but I could find it (probably because I don't know the right vocabulary right now).
Here is my question/ case:
I have two models:
1) Model one: Users
2) Model two: FreelancerData
They are connect through "User has one FreelancerData" and "FreelancerData belongs_to User". The FreelancerData contains three field "daily_rate", "status" and "user_id" (= foreign field).
Now I want to create a Form with form_for, in which I can fill in the FreelancerData for the current user.
I tried the following code, but this does not work
<% provide(:title, "Edit freelancer") %>
<div class="row">
<aside class="span4">
<section>
<%= render ('layouts/profile') %>
</section>
</aside>
<div class="span8">
<h1>Edit your profile</h1>
<h5>Please edit your current working status and your daily rate:</h5>
<%= form_for #user.freelancer_data do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :status %>
<%= f.select :status, ['Busy', 'Available'] %>
<%= f.label :daily_rate, "Daily rate in Euro ('.' and ',' are not allowed)" %>
<%= f.text_field :daily_rate %>
<%= f.submit "Save changes", class: 'btn-large btn-primary' %>
<% end -%>
<h5>If you want to change your picture, you can do it here:</h5>
<%= gravatar_for #user %>
change
</div>
</div>
So my question is: Is it possible to edit data of a associated table FreelancerData through the User Data?
Yup. You can use fields_for. Check out this link.
In my new views page I have:
<% 10.times do %>
<%= render 'group_member_form' %>
<% end %>
Now this form contains the fields: first_name, last_name, email_address and mobile_number. Basically I want to be able to fill in the fields of all the forms in one click which then submits each into the database as a unique row/id.
What would be the easiest way to accomplish this?
Note: The number of times do is called from a variable. Any advice welcome, thanks!
You should have only one form (you should put only fields in the group_member_form partial). In your view you should have something like:
<%= form_tag "/members" do %>
<% 10.times do %>
<%= render 'group_member_form' %>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
and in _group_member_form.html.erb you should have
<%= text_field_tag "members[][first_name]" %>
<%= text_field_tag "members[][last_name]" %>
<%= text_field_tag "members[][email_address]" %>
<%= text_field_tag "members[][mobile_number]" %>
This way, when the form submits, params[:members] in the controller will be an array of member hashes. So, for example, to get the email adress from the fourth member after submitting the form, you call params[:members][3][:email_adress].
To understand why I wrote _group_member_form.html.erb like this, take a glance at this:
http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions.
You can also use accepts_nested_attributes_for in your model, and use fields_for on your form.
Submitting multiple forms, afaik, only javascript, if the forms are remote: true, and you run through each of them and then submit.
$("form.class_of_forms").each(function() {
$(this).submit();
});
Alternatively a more up to date approach using form_with and fields_for, without removing the form into a partial, could be written like this:
<%= form_with (url: end_point_path), remote: true do |form| %>
<% (1..5).each do |i| %>
<%= fields_for 'cart_items'+[i].to_s do |fields|%>
<%= fields.text_field :first_name %>
<%= fields.text_field :last_name %>
<%= fields.email_field :email_address %>
<%= fields.number_field :phone_number %>
<% end %>
<% end %>
<%= form.submit "Submit" %>
<% end %>
I'm using the AASM gem to manage states on one of my models. Right now, I'm using a form_for in a javascript popup to change the state, but it's not working:
<h2>Set the state:</h2>
<%= form_for(#tracker) do |f| %>
<% if #tracker.errors.any? %>
<div id="error_explanation">
<h2>Uh-oh. We've got some problems</h2>
<% #tracker.errors.full_messages.each do |msg| %>
<%= msg %><br />
<% end %>
</div>
<% end %>
This tracker is currently: <%= #tracker.state %><br />
<%= select_tag :state, options_for_select(Tracker::STATEDESCRIPTIONS.map { |event| [event.to_s.humanize, event]}) %>
<%= f.submit %>
<% end %>
What I'd really like to do, though, is contain the form all in a single button, but I'm not sure what to use for that? button_to?
You should use f.select instead of select_tag. That way, the resulting select HTML tag will be associated with the form_for(#tracker), and the chosen state will be correctly mapped to #tracker in the controller action in question.