Trying to do a many to many relations ship.
I have two models, Teams and Matches and i'm doing a many-to-many relations ship but getting this error when trying to save a nested model.
'Couldn't find Team with ID=2 for Match with ID='
I know that my model is new so it doesn't have an id yet, but i'm using the Match.new(params[:match] method which should work.
Team:
class Team < ActiveRecord::Base
attr_accessible :description, :name, :status
attr_protected :id
has_many :matchships
has_many :matches, :through => :matchships
end
Match:
class Match < ActiveRecord::Base
attr_accessible :date, :name
attr_protected :id
has_many :matchships
has_many :teams , :through => :matchships
accepts_nested_attributes_for :teams
end
MatchShips:
class Matchship < ActiveRecord::Base
attr_accessible :match_id, :team_id
belongs_to :match
belongs_to :team
end
Match Controller:
New:
def new
#match = Match.new
#match.teams.build
end
Create:
def create
'failing here' --------> #match = Match.new(params[:match])
#team = Team.find(params[:team_id])
#match.teams << #team
#team.matches << #match
Form:
<%= nested_form_for(#match) do |f| %>
<% if #match.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#match.errors.count, "error") %> prohibited this match from being saved:</h2>
<ul>
<% #match.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<%= f.fields_for :teams, :html => { :class => 'form-vertical' } do |builder| %>
<%= builder.label "Team Name:" %>
<%= builder.autocomplete_field :name, autocomplete_team_name_teams_path, :update_elements => {:id => "##{form_tag_id(builder.object_name, :id)}" },:class => "input-small",:placeholder => "Search" %>
<%= builder.hidden_field :id %>
<% end %>
<%= f.link_to_add raw('<i class="icon-plus-sign"></i>'), :teams, :class => 'btn btn-small btn-primary' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Controller
Related
I have a User model as below,
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable
has_many :company_users
accepts_nested_attributes_for :company_users, :allow_destroy => true
has_many :companies, :through => :company_users
has_many :roles, :through => :company_users
end
and its associated model CompanyUser as below,
class CompanyUser < ActiveRecord::Base
belongs_to :user
belongs_to :company
belongs_to :role
end
I am trying to build the associations as below but it seems like not working
# GET /users/new
def new
#user = User.new
#user.company_users.build
end
View file is as follows,
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<h3>Companies and Roles</h3>
<div class="field">
<% f.fields_for :company_users do |cu| %>
<p>
<%= cu.label :company_id %>
<%= cu.text_field :company_id%>
<%= cu.label :role_id %>
<%= cu.text_field :role_id %>
<%= cu.check_box :_destroy %>
<%= cu.label :_destroy, 'delete' %>
</p>
<% end %>
<p>
<%= f.submit 'Add to user', :name => "add_company_user" %>
<%= f.submit 'Delete from user', :name => "remove_company_user" %>
</p>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I am trying to figure out where it is going wrong.
You are missing a =, therefore the fields_for block isn't rendered to the page. Change this line
<% f.fields_for :company_users do |cu| %>
to
<%= f.fields_for :company_users do |cu| %>
I would like to set a comment form on a post in the "group message show" view.But I have an error message like,
NoMethodError at /group_messages/64
undefined method `group_message_group_message_comments_path' for #<#:0x007f9c0afa0b38>
Could you give me some advice?
☆show.html.erb(group_messages)
<h2>Add a comment</h2>
<%= form_for([#group_message, #group_message_comment]) do |f| %>
<% if #group_message_comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#group_message_comment.errors.count, "error") %> prohibited this group_message_comment from being saved:</h2>
<ul>
<% #group_message_comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %><%# do%>
</ul>
</div><!-- error_explanation-->
<% end %><%# if any?%>
<div class="field">
<%= f.label :member_id %><br />
<%= f.number_field :member_id %>
</div>
<div class="field">
<%= f.label :gmessage_id %><br />
<%= f.number_field :group_message_id %>
</div>
<div class="field">
<%= f.label :group_id %><br />
<%= f.number_field :group_id %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="actions" >
<%= f.submit "Comment"%>
</div>
<% end %><%# form_for>
☆group_messages_controller
def show
if !checklogin? then return end
#group_message = GroupMessage.find(params[:id])
#isme = me? #group_message
#group_message_comment = GroupMessageComment.new
#group_message_comment = GroupMessage.find(params[:id]).group_message_comments.build
respond_to do |format|
format.html # show.html.erb
format.json { render json: #group_message }
end
end
☆GroupMessage model
class GroupMessage < ActiveRecord::Base
attr_accessible :content, :member_id, :group_id
belongs_to :member
belongs_to :group
has_many :group_message_comments
end
☆GroupMessageComment model
class GroupMessageComment < ActiveRecord::Base
attr_accessible :content, :gmessage_id, :group_id, :member_id
belongs_to :member
belongs_to :group_message
end
☆routes.rb
MiniSNS::Application.routes.draw do
resources :group_message_comments
root :to => 'members#login'
match '/groups/join'
resources :group_messages
resources :groups do
resources :group_messages
end
match '/members/new'
resources :index
resources :groups
post 'groups/:id' => 'group#show'
post '/groups/new'
post '/index/index'
match '/members/login'
match '/members/logout'
match '/members/friend'
match 'members/show'
post 'messages/comment'
resources :comments
resources :messages
resources :friends
resources :members
That error message is due to the following line of code:
<%= form_for([#group_message, #group_message_comment]) do |f| %>
This is because you haven't set the correct routing for your models. Can you post your routes.rb too so that I can have a look?
I think you need nested routes, something like:
resources :group_messages do
resources :group_message_comment
end
I have a model Contact which has a many-to-many relationship with Company:
class Contact < ActiveRecord::Base has_many :contactcompanies
has_many :companies, through: :contactcompanies
Company model:
class Company < ActiveRecord::Base
has_many :contactcompanies
has_many :contacts, through: :contactcompanies
ContactCompany:
class ContactCompany < ActiveRecord::Base
belongs_to :company
belongs_to :contact
contacts_controller.rb:
def new
#contact = Contact.new
#all_companies = Company.all
#contact_company = #contact.contactcompanies.build
end
contact create form where I want to have a multiple select for companies:
<%= form_for #contact do |f| %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.label :image_url %>
<%= f.text_field :image_url %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= fields_for(#contact_company) do |cc| %>
<%= cc.label "All Companies" %>
<%= collection_select(:companies, :id, #all_companies, :id, :name, {}, { :multiple => true }) %>
<% end %>
<div class="form-action">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
contacts_path, :class => 'btn' %>
</div>
<% end %>
My issue is when I go to /contacts/new I get this error:
Circular dependency detected while autoloading constant Contactcompany
Any ideas? I'm searching for hours without success. Thanks
You have declared your class as "ContactCompany"
This implies:
has_many :contact_companies
has_many :contacts, through: :contact_companies
Without the underscore, it is looking for a class named Contactcompany, which does not exist.
I'm getting this error:
"ActiveModel::MassAssignmentSecurity::Error in DaysController#create
Can't mass-assign protected attributes: _destroy"
I didn't even know that _destroy is an attribute!
What I have going on:
My model is that I have "Trips" which has many "Days"
In the "Show" view of my Trips model, I'm rendering a partial for a Form to add a new "Day":
<div id="day_form">
<%= render :partial => "day_form", :day => #day %>
</div>
My model:
class Trip < ActiveRecord::Base
attr_accessible :title, :days_attributes
has_many :days
accepts_nested_attributes_for :days, allow_destroy: true
end
class Day < ActiveRecord::Base
attr_accessible :activity_id, :order, :summary, :trip_id, :activities_attributes
belongs_to :trip
has_many :activities, :order => 'position'
accepts_nested_attributes_for :activities, allow_destroy: true
end
When I submit the form, I am getting this Mass Assignment error. Why?
EDIT
The 'Day' Form looks like this:
<%= form_for(#day) do |f| %>
<ul>
<% #day.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset>
<%= f.label :summary, "Day Summary" %><br />
<%= f.text_area :summary, :rows => 1 %><br />
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
module ApplicationHelper
def link_to_remove_fields(name, f)
text_field_tag(:_destroy) + link_to_function(name, "remove_fields(this)")
end
replace f.hidden_field
I'm having real trouble getting my head around editing attributes in has_many through join models. I've set up a very simple app to experiment with; Recipes, Ingredients and Recipe_Ingredients (the join).
Can anyone help with making this work as it should? As it is, it'll pulling through 'qty' from the join model, but not the actual ingredient.
I've put a public repo up that anyone can download to play with: https://github.com/EssentialMusic/Recipes
The models:
class Ingredient < ActiveRecord::Base
attr_accessible :name
has_many :recipe_ingredients, :dependent => :destroy
has_many :recipes, :through => :recipe_ingredients
end
class Recipe < ActiveRecord::Base
attr_accessible :author, :description, :name, :recipe_ingredients_attributes, :ingredients_attributes
has_many :recipe_ingredients, :dependent => :destroy
has_many :ingredients, :through => :recipe_ingredients
accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true
accepts_nested_attributes_for :recipe_ingredients
end
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
attr_accessible :measure, :qty, :special_instructions
end
The form
<%= form_for(#recipe) do |f| %>
<% if #recipe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>
<ul>
<% #recipe.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :author %><br />
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div>
<%= f.fields_for :recipe_ingredients do |ri| %>
<%= ri.text_field :qty %> -
<%= ri.fields_for :ingredients do |i| %>
<%= i.text_field :name %><br>
<% end %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Cheers!!
Substitute the ri with f in the second nested fields_for like this:
<%= f.fields_for :recipe_ingredients do |ri| %>
<%= ri.text_field :qty %> -
<%= **f**.fields_for :ingredients do |i| %>
<%= i.text_field :name %><br>
<% end %>
<% end %>