0
votes

Please help me to figure out the problem. My problem scenario is i have two model say user and project with has many through relationship.

Now i want to create a new user and assign the user with one or more project while creating the user.The project name will be choosen from a dropdown list in the users/_form.html.erb which will be populated from Project model. i want to save the data while creating the new user like this in the projectsusers database table: project_id user_id 1 1 2 1 3 1

when i am creating the new user i get this error "Couldn't find Project with ID=1 for User with ID= "

code

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

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

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

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

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

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

end 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 %>

error log :

Started POST "/users" for 127.0.0.1 at 2013-10-02 16:10:25 +0600 Processing by UsersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"nCsy6E1MuAoMK7hGwAcMNJFVvmq60Bz75lqLLECxb/U=", "user"=>{"name"=>"talha", "projects_attributes"=>{"1380708606908"=>{"id"=>"1", "_destroy"=>"false"}}}, "commit"=>"Create User"} Project Load (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) Completed 404 Not Found in 32ms

ActiveRecord::RecordNotFound (Couldn't find Project with ID=1 for User with ID=): app/controllers/users_controller.rb:43:in new' app/controllers/users_controller.rb:43:increate'

Rendered /Users/maruf/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms) Rendered /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) Rendered /Users/maruf/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (7.1ms)

Thanks in advance guys.........

1
Can you post your controller code?Farley Knight
offcourse i can.Please check i have edited the question and added the controller code.Any help would be appreciated. Thanks for your time.monsur
You need to post all of the code from your controller. What is going on at line 43 in app/controllers/users_controller.rb ?Farley Knight
I have posted all the code of users controller .at line 43 i have this code @user = User.new(params[:user])monsur
Thanks @Farley Knight for your valuable time.I have solve the problem.I have write accepts_nested_attributes_for :project_users, :allow_destroy => true in the user model and then in the form i wrote task_form.collection_select(:project_id, Project.all, :id, :name, :prompt => "please select projects", :multitple => true) after this changes its working great.monsur

1 Answers

1
votes

I have changed the accepts_nested_attributes_for :project_users in the user model and the in the form I used task_form.collection_select(:project_id, Project.all, :id, :name, :multiple => true ) and it rocks.yey......