形成多对多的关系

时间:2014-11-27 17:35:00

标签: ruby-on-rails ruby relationship

我从rails开始。

我试图在两个模型,项目和用户之间实现has_many_belongs_to_many关系,我也做了一个简单的连接表。但我不能做那项工作。我对此感到疯狂。如果有人能提供帮助,我将不胜感激。 我尝试这个解决方案,但它给了我一个错误

Rails : assign multiple params via form_for check_box for HABTM relationship 的Controler:

 def create
 @users = User.find params[:project].delete[:users]
 @project = Project.new(params[:project])
 @project.users = @users

.....

def project_params
  params.require(:project).permit(:client_id, :pstate_id, :name, 
  :description,  :start_date, :deadline, :is_closed, :workload, :users_ids => [])
  end

我的观点是:

 <div class="field">
 <%= f.label :user_ids, "Users" %><br />
 <%= f.collection_select :user_ids, User.order(:name), :id, :name, {}, 
 {multiple: true}    %>
 </div>

现在我犯了这个错误:

ArgumentError in ProjectsController#create
wrong number of arguments (0 for 1)
Extracted source (around line #37):

35
36
37
38
39
40




authorize! :create, @project
#@project = Project.new(project_params)
@users = User.find params[:project].delete[:users]
@project = Project.new(params[:project])
@project.users = @users
#@users = @user.projects

Rails.root: C:/Ruby200/onlineproyect
Application Trace | Framework Trace | Full Trace

app/controllers/projects_controller.rb:37:in `create'

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"wU9MgWv40XMqoy0sDZXd5vtqpFGxXHPmLNm/BmrvVZY=",
 "project"=>{"client"=>"1",
 "pstate"=>"1",
 "name"=>"Projecto 1",
 "description"=>"el ultimo",
 "start_date(1i)"=>"2014",
 "start_date(2i)"=>"11",
 "start_date(3i)"=>"27",
 "deadline(1i)"=>"2014",
 "deadline(2i)"=>"11",
 "deadline(3i)"=>"27",
 "workload"=>"",
 "user_ids"=>["",
 "1",
 "2"]},
"commit"=>"Create Project"}

- 我尝试了很多不同的事情......

由于

1 个答案:

答案 0 :(得分:0)

尝试将其更改为:

@users = User.find params[:project].delete[:user_ids]