rails 4中的has_and_belongs_to_many关联

时间:2013-09-16 20:19:30

标签: associations ruby-on-rails-4

我已完成所有内容http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association,但它不起作用。我在stackoverflow中找到了一些答案,但它仍然不起作用。我已经制作了海报和类型的协会。但是,当我指定海报的类型时,没有任何事情发生。没有添加到DB,海报的视图中没有类型。这是我的PostrsTypes创建表:

class PostersTypes < ActiveRecord::Migration
  def change
    create_table :posters_types, id: false do |t|
      t.integer :poster_id
      t.integer :type_id
    end
    add_index :posters_types, [:poster_id, :type_id]
  end
end

这是海报型号:

class Poster < ActiveRecord::Base
  has_and_belongs_to_many :types
  belongs_to :user

  validates :title, :body, :publish_date, :user_id, :presence => true
end

并输入型号:

class Type < ActiveRecord::Base
  has_and_belongs_to_many :posters

  validates :name, :presence => true
end

任何想法为什么没有在DB中写作?

EDIT。那是我的posters_controller,如果你需要它:

class PostersController < ApplicationController
  before_action :set_poster, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_user!, :except => :index
  # GET /posters
  # GET /posters.json
  def index
    @posters = Poster.all
  end

  # GET /posters/1
  # GET /posters/1.json
  def show
  end

  # GET /posters/new
  def new
    @poster = Poster.new
  end

  # GET /posters/1/edit
  def edit
  end

  # POST /posters
  # POST /posters.json
 def create
    @poster = Poster.new(poster_params)
    @poster.user_id = current_user.id
    respond_to do |format|
      if @poster.save
        format.html { redirect_to @poster, notice: 'Poster was successfully created.' }
        format.json { render action: 'show', status: :created, location: @poster }
      else
        format.html { render action: 'new' }
        format.json { render json: @poster.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posters/1
  # PATCH/PUT /posters/1.json
  def update
    respond_to do |format|
     if @poster.update(poster_params)
        format.html { redirect_to @poster, notice: 'Poster was successfully updated.' }
        format.json { head :no_content }
     else
        format.html { render action: 'edit' }
        format.json { render json: @poster.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posters/1
  # DELETE /posters/1.json
  def destroy
    @poster.destroy
    respond_to do |format|
      format.html { redirect_to posters_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_poster
      @poster = Poster.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def poster_params
      params.require(:poster).permit(:title, :body, :publish_date, :type_ids)
    end
end

2 个答案:

答案 0 :(得分:2)

尝试使用:type_ids =&gt; []

def poster_params    
  params.require(:poster).permit(:title, :body, :publish_date,:type_ids=>[])
end

这对我有用

答案 1 :(得分:0)

所以你现在得到了@poster。在您上面调用.save后,它会保存,或者会在您的模型中出现一些错误。检查您的验证,然后输出它们,例如使用@poster.errors(在您的new - 操作中,因为如果保存失败则会呈现此内容)并查看错误。