Is possible to utilize the habtm checkboxes on create action?
because this:
<%= hidden_field_tag "product[size_ids][]", nil %>
<% Size.order(:size).each do |size| %>
<li> <%= check_box_tag "product[size_ids][]", size.id, Product.size_ids.include?(size.id), id: dom_id(size) %>
<%= label_tag dom_id(size), size.size %>
</li>
<% end %>
was on update and was working since was brought to create page rails spits out the
undefined method `size_ids' for #
so, have a way to utilize the habtm on a create action?
Since you're likely dealing with one item, you probably mean:
#product.sizes_ids
The Product model doesn't have a direct association with any sizes, it's only instances of it that do.
use collection check boxes getting all of the model with ids exemple:
<%=p.collection_check_boxes :size_ids, Size.all, :id, :size %>
Related
I have a #friend model that has_and_belongs_to_many #interests and vice versa. Each interest has a name:string. How do I show all the interests by their name next to each friend?
I tried
friend.interests.count
which shows the correct number, but for
friend.interests.first
the result is
#<Interest:0x00007f959e103250>
How do I display the name of this interest from the database in a view?
<%= friend.interests.count %>
<%= friend.interests.first %>
You can get the name of the interest, just by accessing the friend.interests.first.name. And for listing all the interests you can iterate and show the name of them.
<% friend.interests.each do |interest| %>
<%= interest.name %>
<% end %>
Just put the attribute after the object:
<%= friend.interests.first.name %>
Hello I am trying to create a form in which Model A has many and belong to itself. I tried using nested_form and no luck yet.
I want to create nested form for just Model A.
Can anyone suggest me correct path ?
Without more details on your models and structure it's hard to help, maybe include some code too?
For nested models you typically do something like this:
<%= form_for #model_a do |model_a_form| %>
<%= model_a_form.text_field :attribute_1 %>
<%= model_a_form.text_field :attribute_2 %>
<%= fields_for :child_models, #model_a.child_models do |child_models_fields| %>
<%= child_models_fields.text_field :attribute_1 %>
<%= child_models_fields.text_field :attribute_2 %>
<% end %>
<%= model_a_form.submit %>
<% end %>
See https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for for more info.
There is 'FoodType' model which are describes types of food in restaurants. I need to make view for creating a new restaurant, and I need to have list of checkboxes in order to allow user to setup types of food for each restaurant. I want to have something like this:
<% FoodType.all.each do |food_type| %>
...
<div class="row">
<%= f.check_box :food_types[0] %>
</div>
...
<% end %>
I want to have parameters like params[restaurant][food_types][0] = true in order to make some actions after creating. Please, tell me, how can I do it? Thanks in advance.
Presumably you have a join table which joins restaurants and food types? Let's say that you have one called restaurant_food_types (with a model RestaurantFoodType), which has restaurant_id and food_type_id?
You will then have this association in restaurants:
Restaurant < ActiveRecord::Base
has_many :restaurant_food_types
has_many :food_types, :through => :restaurant_food_types
This will give you the method .food_type_ids which you can call on a restaurant to set the joins. It's this method that you should hook into in your form: it expects an array of ids, so you need to set up an array-style parameter (one where the name ends in []) You may need to use check_box_tag rather than .check_box, to access an array-style parameter name: i would do this:
<% form_for #restaurant do |f| %>
<% FoodType.all.each do |food_type| %>
...
<div class="row">
<%= check_box_tag "restaurant[food_type_ids][]", food_type.id, #restaurant.food_type_ids.include?(food_type.id) %><%= food_type.name %>
</div>
...
<% end %>
<%= f.submit "Save" %>
<% end %>
Like i say i'm using a check_box_tag here but there might be a nicer way to hook into the food_type_ids method.
How can i bring 2 variables into the view. I am newbie in Ruby on rails.
What will the sytax to bring 2 or more values into a view.
<%= form_for(#user) do |f| %>
EDIT :
<%= form_for(#user) do |f| %>
prohibited this user from being saved: </h2>
<u1>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</u1>
</div>
<% end %>
<%= debug #user %>
<div class = "field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
Let's say that in the above code I want to print values from 2 objects and also submit them. How can I do that ?
Well, if you need to use 2 variables, I think it would be better to use two separate forms because they are unrelated. If two variables have relations to each other, you should use accepts_nested_attributes_for and fields_for to do work.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
from your above comment "I am talking about loading 2 model into one form" I think you need to use two model in one form so rails cast produce good episode on using nested model for form .
You can use accepts_nested_attributes_for for eg. for survey is one model and question is another model you can use question as nested_attributes in survey and same you can use answers model in survey model in same form.
For more you can read following link.
http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast
I hope this will help you.
Thanks.
I have a subject model with two fields - "name" and "level". I want to be able to visit "/subjects#new" and add 10 subjects objects at once. How can I do this in formtastic.
In regular forms I would do this:
<% #subjects.each do |s| %>
<% fields_for "subject[#{s.id}]", s do |f|%>
<%= f.name%>
<%= f.level %>
<% end %>
<% end %>
Change fields_for to semantic_fields_for for and it should work.