2012-02-25 2 views
1

태그와 사용자의 다 대다 연관이 있습니다. 내가 가입 테이블 내 업데이트되지 않는 태그를 만들 때, 나는 컨트롤러 뭔가가 있다고 생각 ..Ruby on Rails HABTM 연결이 조인 테이블을 업데이트하지 않습니다.

태그 모델

class Tag < ActiveRecord::Base 
    validates :tag, :presence => true 
    has_and_belongs_to_many :user, :join_table => "tags_users" 
end 

사용자 모델

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

    attr_accessible :email, :password, :password_confirmation,:remember_me 

    has_and_belongs_to_many :tag, :join_table => "tags_users" 
end 

tags_controller

class TagsController < ApplicationController 
    before_filter :authenticate_user!, :except => [:index, :show] 

    def create 
    @tag = Tag.new(params[:tag]) 

    respond_to do |format| 
     if @tag.save 
     format.html { redirect_to tags_path, notice: 'Tag was successfully created.' } 
     format.json { render json: @tag, status: :created, location: @tag } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @tag.errors, status: :unprocessable_entity } 
     end 
    end 
    end 
end 

답변

1

작성한 tag 그리고 내가 볼 수있는 현재 사용자. 또한

@tag = Tag.new(params[:tag]) 
@tag.users << current_user 

일반적 has_and_belongs_to_many의 인수를 복수로하고, 예를 들어 has_and_belongs_to_many :users