0

请帮我找出问题所在。我的问题场景是我有两个模型说用户和项目有很多通过关系。

现在我想创建一个新用户并在创建用户时为用户分配一个或多个项目。项目名称将从 users/_form.html.erb 的下拉列表中选择,该下拉列表将从项目模型中填充。我想在projectusers数据库表中创建这样的新用户时保存数据:project_id user_id 1 1 2 1 3 1

当我创建新用户时,我收到此错误“找不到 ID=1 的用户 ID= 的项目”

代码

class User < ActiveRecord::Base
  attr_accessible :name, :projects_attributes
  has_many :project_users, :class_name => 'Projectuser'
  has_many :projects, through: :project_users
  accepts_nested_attributes_for :projects, :allow_destroy => true
end


class Project < ActiveRecord::Base
  attr_accessible :name
  has_many :project_users
  has_many :users, :through => :project_users

end


class Projectuser < ActiveRecord::Base
  attr_accessible :project_id, :user_id
  belongs_to :user
  belongs_to :project
end

 controller
   class UsersController < ApplicationController

# GET /users # GET /users.json def index @users = User.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @users }
end

结尾

# GET /users/1 # GET /users/1.json def show @user = User.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @user }
end

结尾

# GET /users/new # GET /users/new.json def new @user = User.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @user }
end

结尾

# GET /users/1/edit def edit @user = User.find(params[:id]) end

# POST /users # POST /users.json def create @user = User.new(params[:user])

@user.project_users.build
respond_to do |format|
  if @user.save

    #@user.project_users.update_attributes(params[][:projects_attributes])
    format.html { redirect_to @user, notice: 'User was successfully created.' }
    format.json { render json: @user, status: :created, location: @user }
  else
    format.html { render action: "new" }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end

结尾

# PUT /users/1 # PUT /users/1.json def update @user = User.find(params[:id])

respond_to do |format|
  if @user.update_attributes(params[:user])
    format.html { redirect_to @user, notice: 'User was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end

结尾

# 删除 /users/1 # 删除 /users/1.json def destroy @user = User.find(params[:id]) @user.destroy

respond_to do |format|
  format.html { redirect_to users_url }
  format.json { head :no_content }
end

结束结束

<%= nested_form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being         saved:</h2>

      <ul>
      <% @user.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>
  <%= f.fields_for :projects do |task_form| %>
    <%= task_form.collection_select(:id, Project.all, :id, :name, :include_blank => true ) %>
    <%= task_form.link_to_remove "Remove this task" %>
  <% end %>
  <p><%= f.link_to_add "Add a task", :projects %></p>
  </div>
  <div class="actions">
  <%= f.submit %>
  </div>
<% end %>

错误日志:

在 2013 年 10 月 2 日 16:10:25 +0600 开始 POST "/users" for 127.0.0.1 由 UsersController#create 作为 HTML 参数处理:{"utf8"=>"✓", "authenticity_token"=>"nCsy6E1MuAoMK7hGwAcMNJFVvmq60Bz75lqLLECxb/ U=", "user"=>{"name"=>"talha", "projects_attributes"=>{"1380708606908"=>{"id"=>"1", "_destroy"=>"false"}} }, "commit"=>"Create User"} 项目负载 (0.1ms) SELECT "projects".* FROM "projects" INNER JOIN "projectusers" ON "projects"."id" = "projectusers"."project_id" WHERE "projectusers"."user_id" 为 NULL 并且 "projects"."id"IN (1) Completed 404 Not Found in 32ms

ActiveRecord::RecordNotFound(找不到 ID=1 的用户 ID= 的项目):app/controllers/users_controller.rb:43:in new' app/controllers/users_controller.rb:43:increate'

渲染 /Users/maruf/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms) 渲染 /Users/maruf/ .rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms) 渲染/Users/maruf/.rvm/gems/ruby -1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb 在救援/布局中(7.1ms)

先谢谢各位了。。。。。。

4

1 回答 1

1

我已经更改了用户模型中的accepts_nested_attributes_for :project_users 和我使用task_form.collection_select(:project_id, Project.all, :id, :name, :multiple => true ) 的表单,它是rocks.yey .... ..

于 2013-10-04T10:47:59.737 回答