请帮我弄清问题。 我的问题场景是我有两个模型说用户和项目有很多关系。
现在我想创建一个新用户,并在创建用户时为用户分配一个或多个项目。项目名称将从users / _form.html.erb的下拉列表中选择,该列表将从Project填充模型。 我想在projectsusers数据库表中创建这样的新用户时保存数据: 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指数 @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 /编辑 def编辑 @user = User.find(params [:id]) 端
#POST / users #POST /users.json def创建 @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更新 @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
端
#DELETE / users / 1 #DELETE /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-02 16:10:25 +0600开始发布“/ users”for 127.0.0.1 UsersController处理#create as HTML 参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“nCsy6E1MuAoMK7hGwAcMNJFVvmq60Bz75lqLLECxb / U =”,“user”=&gt; {“name”=&gt;“talha”,“projects_attributes”=&gt; { “1380708606908”=&gt; {“id”=&gt;“1”,“_ destroy”=&gt;“false”}}},“commit”=&gt;“创建用户”} 项目加载(0.1ms)SELECT“projects”。* FROM“projects”INNER JOIN“projectusers”ON“projects”。“id”=“projectusers”。“project_id”WHERE“projectusers”。“user_id”IS NULL AND“projects “。”id“IN(1) 已完成404在32毫秒内找不到
ActiveRecord :: RecordNotFound(对于ID =的用户,找不到ID = 1的项目):
app / controllers / users_controller.rb:43:new'
app/controllers/users_controller.rb:43:in
创建'
呈现/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) 在救援/布局(7.1ms)内呈现/Users/maruf/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb
先谢谢你们.........
答案 0 :(得分:1)
我更改了用户模型中的accepts_nested_attributes_for:project_users 在我使用的形式task_form.collection_select(:project_id,Project.all,:id,:name,:multiple =&gt; true) 并且它摇滚...