vendredi 20 mars 2015

data is not saved in joins table

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

Aucun commentaire:

Enregistrer un commentaire