0

I am working on a website, where a user can have multiple projects and multpile users can contribute to a single project. I have a project model

class Project < ActiveRecord::Base
  #associations
  has_and_belongs_to_many :users
end

and a users model

class User < ActiveRecord::Base
  #associations
  has_and_belongs_to_many :projects
end

and I have created a joins table by the name - :projects_users everything works fine when I run the code on rails console. but when I try to do the save thing in the controller, the data is not being saved in the joins table. Code for the controller

please help

class ProjectsController < ApplicationController

 def new
   @project = Project.new
 end

 def create
   @user = User.find(session[:user_id])
   @project = Project.new(project_params)   
     if @project.save
       @project.users << @user
       redirect_to @project
     else 
       flash[:error] = "Project has not been created due to some error"
       render 'new'
     end
 end

  private

  def project_params
    params.require(:project).permit(:name,:description)
  end
end
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
dev
  • 127
  • 11
  • try `@project = @user.projects.create(project_params)` or ``@project = @user.projects.new(project_params)`` – Sonalkumar sute Mar 21 '15 at 05:12
  • @Sontya throws and undefined method projects error. and the append (<<)operator is working fine in console.Can you guide about why is it not working in the controller – dev Mar 21 '15 at 05:17
  • do you have `session[:user_id]`, you will not be getting `@user` that's why it is saying `undefined` method projects – Sonalkumar sute Mar 21 '15 at 05:19
  • @Sontya - Can u suggest the necessary changes ? – dev Mar 21 '15 at 05:33

2 Answers2

0

Try using nestes_attributes_for

class Answer < ActiveRecord::Base
    belongs_to :question
end


class Question < ActiveRecord::Base
    has_many :answers
    accepts_nested_attributes_for :answers, allow_destroy: true
end

Controlller

def new
    @question = Question.new
    @question.answers.build
end

def create
    @question = Question.new(question_params)
    respond_to do |format|
        if @question.save
            format.html { redirect_to @question, notice: 'question was successfully created.' }
            format.json { render action: 'show', status: :created, location: @question }
        else
            format.html { render action: 'new' }
            format.json { render json: @question.errors, status: :unprocessable_entity }
        end
    end
end

def question_params
    params.require(:question).permit(:name, :description, answers_attributes:[:content, :id, :question_id])
end        

You form should look like this

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

      <ul>
      <% @question.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 class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>

  <%= f.fields_for :answer do |builder| %>
     <%= builder.label :content %>
     <%= builder.text_area :content %>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
Sonalkumar sute
  • 2,565
  • 2
  • 17
  • 27
0

just changed

@project.users << @user

to

@user.projects << @project

and it started working.Dont know the reason yet but its working

dev
  • 127
  • 11