Multiple fields not display in single form and model Rails - ruby-on-rails

I have model product. I want to create multiple record in single model (product), but fields not display in view
#controller
#products = Array.new(3){ Product.new }
# view
<%= form_tag create_product_path, :method => :post, :class => "form-horizontal", 'role' => "form" do %>
<% #products.each_with_index do |product, index| %>
<% fields_for "products[#{index}]", product do |f| %>
<%= f.text_field :date %>
<%= f.text_field :name %>
<% end %>
<% end %>
<%= submit_tag "Submit", :class => "btn btn-primer" %>
Look at this screenshoot, fields not appear. Can anyone tell me, why new form method using array not appear?

Don't forget about <%= on fields_for
read about fields_for
rails 2.x - 3.0
<% fields_for "products[#{index}]", product do |f| %>
rails > 3.1.x
<%= fields_for "products[#{index}]", product do |f| %>

Related

Model validation errors not appearing for nested model in simple_form

So I have a simple form for entering "brand" and "model" through a single submit button, as below:
<%= simple_form_for #brand, :html => { :class => 'form-horizontal' } do |m| %>
<fieldset>
<legend><%= controller.action_name.capitalize %> /Brand</legend>
<%= m.input :name %>
<%= m.simple_fields_for :models, Model.new do |p| %>
<%= p.input :name %>
<% end %>
<div class="form-actions">
<%= m.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', brands_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
I have name:string in my schema for both brand and model.. and validates_presence_of :name in both models.. The form DOES work for creating the brand and model simultaneously but my error "can't be blank" only shows up for the brand field.
Thanks for any help with this issue.
Realized what I was doing wrong. Needed to ALSO put #model = Model.new inside of the controller and put <%= m.simple_fields_for :models, #model do |p| %>

Rails checkboxes and serialized data

I'm trying to figure out whats the best way to get checkboxes to properly show their current state. This is what I have in my form
<%= form_for #user, :url => user_notification_preferences_path(#user), :method => :put do |f| %>
<%= f.fields_for :notification_preferences, #user.notification_preferences do |p| %>
<%= p.check_box :notify_on_friend_post %>
<%= p.check_box :notify_on_friend_post %>
<%= p.check_box :notify_on_friend_request %>
<%= p.check_box :notify_on_friend_comment %>
<% end %>
<%= f.submit %>
<% end %>
notification_preferences is a serialized hash on my user model
class User < ActiveRecord::Base
serialize :notification_preferences, Hash
My issue that is no matter what I try, I can not get the check boxes to reflect the existing state of the hash values. IE, if the hash already contains :notify_on_friend_post => 1, then the check box for that value should be checked.
The form posts the data fine, and I'm able to update my model as well.
Update
using check_box_tag I can get this to work
<%= p.hidden_field :notify_on_friend_post, :value => "0" %>
<%= check_box_tag "user[notification_preferences][notify_on_friend_post]", "1", #user.notification_preferences[:notify_on_friend_post] == "1" ? true : false %>
ugly but working, still hoping I'm missing something very obvious
I ran into this problem and solved it in a simpler way.
<%= form_for #user do |f| %>
<%= f.fields_for :notifications, #user.notifications do |n| %>
<%= n.check_box :new_task, checked: #user.notifications[:new_task] == "1" %>
<% end %>
<%= f.submit %>
<% end %>
In this way you let the magic of check_box to the work and don't need to have a hidden_field because Rails will provide one for you. Still unsure why you need a "checked" field, but it seemed to not work without one.
Try something like this:
<%= form_for #user, :url => user_notification_preferences_path(#user), :method => :put do |f| %>
<%= check_box_tag "user[notification_preferences][]", :notify_on_friend_post, #user.notification_preferences.try(notify_on_friend_post) %>
<%= f.submit %>
<% end %>

RoR field set on form_for

How do you add a field set to a form_for method?
You can use field_set_tag. For example, using a generic 'user' object
For Rails 2.3.x
<% form_for(#user) do |f| %>
<% field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
And for Rails 3.0.0:
<%= form_for(#user) do |f| %>
<%= field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
You need to have a new object or get an existing object from your dc since it is a 'form for' and then you create a form builder f and call methods on that form builder such as the following:
<% form_for(#object) do |f| %>
<%= f.text_field :method_name %>
<% end %>

Nested Rails forms and using label_tag, checkbox_tag and other form_tag functions

In regular forms in Ruby on Rails, if using form_for to build a model, as the API docs state, form_for doesn't create an exclusive scope, and it's possible to use form_tag functions within the form_for form.
For example:
<% form_for :person, #person, :url => { :action => "update" } do |f| %>
First name: <%= f.text_field :first_name %>
Admin? : <%= check_box_tag "person[admin]", #person.company.admin? %>
<% end %>
However, in a nested form, the labels and fields have names that are automatically generated by Rails to be associated with a given nested model and not to overlap if multiple nested models are created at once. Is it possible to still use the form_tag functions?
I'd like to do something like this:
<% person_form.fields_for :children do |child_form| %>
Name: <%= child_form.text_field :name %>
Give up for Adoption?: <%= check_box_tag "adoption_" + child_form_index, false %>
<% end %>
However, I don't know how to get access to the child_form's index to ensure that check_box_tag has a unique value if there are multiple children.
Is what I'm trying to do possible?
See the docs for fields_for under one-to-many.
It looks to me like basically you can just use each (or each_with_index) and pass the block variable along with the symbol:
<% form_for #person, :url => { :action => "update" } do |person_form| %>
...
<% #person.children.each_with_index do |child, index| %>
<% person_form.fields_for :children, child do |children_fields| %>
Name: <%= children_fields.text_field :name %>
Give up for Adoption?: <%= check_box_tag "adoption_" + index, false %>
<% end %>
<% end %>
<% end %>
Of course, you'll have to handle the "offer for adoption" login on your own.

How to loop all the elements in the DB using RoR?

I use this to loop the products...
<% form_for :product, #products, :url => { :action => "add_to_cart" } do |f| %>
<%= product.title %>
<%= product.price %>
<%= submit_tag 'Make Order' %>
<% end %>
In my DB, I have
product:
title:abc price:874
title:cde price:98
title:efg price:18
but I can only get the efg 18.0 in my result, I miss other records on my result,
any ideas on that?
I suppose that you need an extra form for each product, (based on the add_to_cart action).
form_for helper generates a form for one object, so you will need to iterate through your objects and create a form for each one.
Something like that is probably what you need:
<% for product in #products %>
<% form_for product, :url => { :action => "add_to_cart" } do |f| %>
<%= product.title %>
<%= product.price %>
<%= f.submit 'Make Order' %>
<% end %>
<% end %>
form_for creates a form for a single object. If you want to create a form for multiple objects, I suggest you take a look at this question: Multiple objects in a Rails form

Resources