Reputation: 853
I am new to rails and I am trying to create an app where users can belong to many teams and teams contain many users. Here are my model classes and the migration pages. I am trying to make a signup page where you can click on different teams to assign the user. Having a default none team would be good to implement.
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :teams
accepts_nested_attributes_for :teams
end
class Team < ActiveRecord::Base
has_many :users
attr_accessible :name
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.references :teams
t.timestamps
end
end
end
class CreateTeams < ActiveRecord::Migration
def change
create_table :teams do |t|
t.string :name
t.references :users
t.timestamps
end
end
end
Here is the code in my controller for the creation method
def create
@users = User.all
@user = User.new(params[:user])
if @user.save
flash.keep[:success] = @user.name + " Added to list of Users!"
redirect_to users_path
else
render 'index'
end
end
Here is the code in my view file
<% for team in @teams %>
<%= check_box_tag 'user[team]', team.name, @user.team_name.include?(team.name)%>
<%= team.name -%>
<% end %>
However the output is just "Team". I am not sure if the value is actually getting passed onto the object.
edit: I just needed to read the checkbox doc sheet. The parameter that was causing the trouble could just have been turned into true and false for the initlized value.
Upvotes: 0
Views: 72
Reputation: 3999
Maybe because here is no `team_name method for the user object? Did you mean perhaps?
@user.teams.map(&:name).include?(team.name)
Or even better
@user.teams.include?(team)
Upvotes: 1