I have a form from which a project is created. The form collects data for two tables; ‘projects’ and ‘projects_users’. The last one
is a relational tabel which is used for storing which users that are members of the project (choosen in the form).
Creating a new project works fine, but when it comes to creating new posts in ‘projects_users’ it fails:
uninitialized constant ProjectsController::Projects_users
First of all, am I thinking in the right way (see the code below)?
What I do is that I extract the members array from params[:project], that’s retrieved from the form. Then I’m creating the project in the db, and when that is done I iterate through the members array (which contains of user_id’s) and creates the posts in the db for ‘projects_users’.
What am I doing wrong? Is there a better way to achieve what I want?
projects controller:
class ProjectsController < ApplicationController
def new
@project = Project.new
@users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
end
def create
@members = params[:project].delete(:members)
@project = Project.new(params[:project].merge(:user_id => current_user.id))
if @project.save
@members.each do |member|
@project_members = Projects_users.new
@project_members.project_id = @project.project_id
@project_members.user_id = member.user_id
end
redirect_to @project
else
@users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
render :new
end
end
end
new.html.erb:
<%= form_for @project do |f| %>
<div class="alert alert-block">
<%= f.error_messages %>
</div>
<div class="text_field">
<%= f.label :title%>
<%= f.text_field :title%>
</div>
<div class="text_field">
<%= f.label :description%>
<%= f.text_field :description%>
</div>
<div class="dropdown">
<%= f.label :start_date%>
<%= f.date_select :start_date %>
</div>
<div class="dropdown">
<%= f.label :end_date%>
<%= f.date_select :end_date %>
</div><br/>
<span class="help-block">Välj användare som ska ingå ingå i projektet.</span>
<div class="checkbox">
<% @users.each do |user| %>
<%= check_box_tag "project[members][]", user.id, '1', :id => "user_#{user.id}" %>
<%= label_tag "user_#{user.id}", user.first_name + ' ' + user.last_name, :class => "checkbox" %>
<% end %>
</div>
</div>
<div class="submit">
<%= f.submit "Spara" %>
</div>
<% end %>
project model:
class Project < ActiveRecord::Base
has_and_belongs_to_many :users, :class_name => 'User'
belongs_to :user
has_many :tickets, :dependent => :destroy
// validation...
# Setup accessible (or protected) attributes for your model
attr_accessible :user_id, :title, :description, :start_date, :end_date
end
users model:
class User < ActiveRecord::Base
attr_accessible :first_name, :last_name, :email, :password
has_and_belongs_to_many :projects
has_many :tickets
... other code
end
projects_users table:
project_id
user_id
projects table:
user_id (the user that creates the project = admin)
title
description
start_date
end_date
In the controller you are using
Projects_users.newthat means the new method forProjects_usersmodel and as you are using HABTM association the join model doesn’t exist. If you want to use the join/associated model then you must usehas_many :throughassociation. But in case you just want to associate preexisting users to projects you can do it following wayOR
@member will be an array in your case not a hash. It will contain all the checked users’ ids. So you don’t need to have
member.user_idas it is not a hash.