In this scenario I can insert values to the Member table and the Club table. But there is a field called :task in the memberships table that I want to submit a value to, and in the Memberships table member_id and club_id are inserted automatically by Rails. How do I include the task field in the form below? Thank you in advance.
View/form:
<%= form_for #member ,:url=>{:action =>"create"} do |f| %>
<%= f.text_field :email %>
<%= f.fields_for :clubs do |s| %>
<%= s.text_field :name %>
<% end %>
<%= f.submit "submit" %>
<% end %>
Models
class Member < ActiveRecord::Base
has_many :clubs ,:through=> :memberships
has_many :memberships
accepts_nested_attributes_for :clubs
attr_accessible :clubs_attributes
end
class Club < ActiveRecord::Base
has_many :members ,:through=>:memberships
has_many :memberships
end
class Memberships < ActiveRecord::Base
belongs_to :Member
belongs_to :Club
end
<%= f.fields_for :memberships do |m| %>
<%= m.text_field :task %>
<%= m.fields_for :clubs do |s| %>
<%= s.text_field :name %>
<% end %>
<% end %>
Related
class User < ActiveRecord::Base
has_many :tasks
has_many :task_items, through: :task
accepts_nested_attributes_for :task
accepts_nested_attributes_for :task_item
end
class Task < ActiveRecord::Base
belongs_to :user
has_many :task_item
end
class TaskItem < ActiveRecord::Base
belongs_to :task
end
I am able to get & save form data for Task using form_for in the user form.
<%= fields_for :user, user do |f| -%>
<%= f.fields_for :task do |builder| %>
<%=builder.text_field :name%>
<%end%>
<% end %>
I want to accept attributes for Task as well as TaskItem in the User form itself using form_for.
Unable to figure out how to do this.
I tried with:
<%= fields_for :user, user do |f| -%>
<%= f.fields_for :task do |builder| %>
<%=builder.text_field :name%>
<%= f.fields_for builder.object.checklist do |builder_1| %>
<%builder_1.object.each do |bb|%>
<%= bb.check_box :completed%>
<%end%>
<%end%>
<%end%>
It gives undefined method `check_box' for #
I want to be able to create a User,its task & task item records all using one form.
Any solutions are welcome.
You have many pluralization errors. Check these changes:
class User < ActiveRecord::Base
has_many :tasks
has_many :task_items, through: :tasks #not task
accepts_nested_attributes_for :tasks #not task
accepts_nested_attributes_for :task_items #not task_item
end
class Task < ActiveRecord::Base
belongs_to :user
has_many :task_items #not task_item
end
class TaskItem < ActiveRecord::Base
belongs_to :task
end
Then, the view:
<%= fields_for :user, user do |f| %>
<%= f.fields_for :tasks do |builder| %>
<%= builder.text_field :name %>
<%= builder.fields_for :task_items do |ti| %>
<%= ti.check_box :completed %>
<% end %>
<% end %>
<% end %>
A User has many Skills through UserSkills.
A Skill has many Users through UserSkills.
In my new user form, I'm able to add checkboxes for skills but if I add a Proficiency (string) attribtue to the UserSkills model, how can I include this?
My current code:
<%= f.label :skills %>
<%= hidden_field_tag "user[skill_ids][]", nil %>
<% Skill.all.each do |skill| %>
<%= check_box_tag "user[skill_ids][]", skill.id, #user.skill_ids.include?(skill.id), id: dom_id(skill) %>
<%= link_to skill.skilltitle, skill_path(skill.id) %>
class User < ActiveRecord::Base
has_many :user_skills
accepts_nested_attributes_for :user_skills
end
class UserSkill < ActiveRecord::Base
belongs_to :user
belongs_to :skill
accepts_nested_attributes_for :skill
accepts_nested_attributes_for :user
end
... meanwhile.. in your form
<%= form_for #user do |f| %>
<%= f.fields_for :user_skills do |f2| %>
<%# add your user_skill attributes here %>
<%= f2.check_box :proficiency %>
<%= f2.fields_for :user do |f3| %>
<%# user attributes to go here %>
<% end %>
<% end %>
So there is a plethora of questions about the "uninitialized constant" error, and it's almost always due to an incorrectly specified association (e.g. plural model names instead of singular, incorrectly writing your association inside the model, etc). My models and form look spotless, so maybe this is something new (or I'm blind)?
A "user" has one "move". A "move" has many "neighborhood_preferences", and through this, many "neighborhoods".
Models:
class User < ActiveRecord::Base
has_one :move
accepts_nested_attributes_for :move, allow_destroy: true
end
class Move < ActiveRecord::Base
belongs_to :user
has_many :neighborhood_preferences
has_many :neighborhoods, through: :neighborhood_preferences
accepts_nested_attributes_for :neighborhood_preferences, allow_destroy: true
end
class NeighbhoodPreference < ActiveRecord::Base
belongs_to :neighborhood
belongs_to :move
end
class Neighborhood < ActiveRecord::Base
belongs_to :city
has_many :neighborhood_preferences
has_many :moves, through: :neighborhood_preferences
end
View:
<%= simple_form_for(#user, :html => { class: :form } ) do |u| %>
<%= u.fields_for :move do |m| %>
<div>
<%= m.label :start_date %>
<%= m.date_field :start_date %>
</div>
<div>
<%= m.label :end_date %>
<%= m.date_field :end_date %>
</div>
<div>
<%= m.label :min_price %>
<%= m.text_field :min_price %>
</div>
<div>
<%= m.label :max_price %>
<%= m.text_field :max_price %>
</div>
<%= m.association :neighborhood_preferences %>
<% end %>
<%= u.submit "Save Changes" %>
<% end %>
There is a typo in class name NeighbhoodPreference.
I'm trying to get Order and Products linked together via a join table called orders_products, an order has many :products though :order_products and a product has many :orders though :order_products, however when I save the form and look in the view no products are displayed for that order.
new.html.erb for Order
<%= simple_form_for #order do |f| %>
<%= f.input :order_number %>
<%= hidden_field_tag "order[product][]", nil %>
<% Product.all.each do |product| %>
<%= check_box_tag "order[product_ids][]", product.id, #order.product_ids.include?(product.id), id: dom_id(product) %>
<%= label_tag dom_id(product), product.name %><br>
<% end %>
<%= f.submit %>
order.rb
class Order < ActiveRecord::Base
belongs_to :customer
has_many :order_products, class_name: "OrderProduct"
has_many :products, through: :order_products
end
product.rb
class Product < ActiveRecord::Base
has_many :order_products, class_name: "OrderProduct"
has_many :orders, through: :order_products
end
order_product.rb
class OrderProduct < ActiveRecord::Base
belongs_to :product
belongs_to :order
end
show.html.erb for Order
<div class="well">
<%= "Order number " + #order.order_number %>
<% #order.products.each do |product| %>
<%= product.name %>
<% end %>
</div>
I have recipe and ingredient in a many to many relation.
I have defined the following commands in my presentation.
<div>
<%= render :partial => 'ingredients/form',
:locals => {:form => recipe_form} %>
</div>
the partial begins with
<%= form_for(#ingredient) do |ingredient_form| %>
but received #ingredient nill.
Then I tried
<%= recipe_form.fields_for :ingredients do |builder| %>
<%= render 'ingredient_fields', f: builder %>
<% end %>
where my render was
<p class="fields">
<%= f.text_field :name %>
<%= f.hidden_field :_destroy %>
</p>
but nothing was printed.
then I tried
<% #recipe.ingredients.each do |ingredient| %>
<%= ingredient.name %>
<% end %>
and only then all of the ingredients were printed.
What was I doing wrong in the previous tries ?
Thank you.
my ingredient recipe relation defined as follows
class Ingredient < ActiveRecord::Base
has_many :ingredient_recipes
has_many :recipes, :through => :ingredient_recipes
...
class Recipe < ActiveRecord::Base
has_many :ingredient_recipes
has_many :ingredients, :through => :ingredient_recipes
...
accepts_nested_attributes_for :ingredient_recipes ,:reject_if => lambda { |a| a[:content].blank?}
class IngredientRecipe < ActiveRecord::Base
attr_accessible :created_at, :ingredient_id, :order, :recipe_id
belongs_to :recipe
belongs_to :ingredient
end
You don't exactly specify what you are trying to do, so I am presuming you have a page that shows a recipe, with many ingredients that can be edited and added to. In your controller you have something like:
class RecipeController < ApplicationController
def edit
#recipe = Recipe.find(params[:id]
end
end
I am also presuming that you are looking to have a form that post backs to the create action. I therefore think you want a form like this:
<%= form_for #recipe do |form| %>
<%= label_for :name %>
<%= text_field :name %>
<%= form.fields_for :ingredients do |ingredients_fields| %>
<div class="ingredient">
<%= f.text_field :name %>
<%= f.hidden_field :_destroy %>
</div>
<% end %>
<% end %>
Also, change your recipe to accept nested attributes for ingredients, not ingredient_recipes:
class Recipe < ActiveRecord::Base
has_many :ingredient_recipes
has_many :ingredients, :through => :ingredient_recipes
...
accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:content].blank?}
And finally, add attr_accessible for your content:
class Ingredient < ActiveRecord::Base
attr_accessible :content
...
Does that work for you?