0

私は次の関連付けと関連するコントローラーを持っています。私のフォームでは、すべてのフィールドを必要に応じて追加しています。しかし、アイテムを作成しようとすると、Ratings item can't be blankというエラーが表示されます。Rails 4.0を使用しています。私はこれを広範囲に検索しましたが、私が間違っていることをまだ見つけることができませんでした。ありがとうございました!

class Item < ActiveRecord::Base
  has_many :ratings, dependent: :destroy
  accepts_nested_attributes_for :ratings, :allow_destroy => true
  validates :name , :length => { minimum: 3 }
  validates :category , :length => { minimum: 3 }
end

class Ratings < ActiveRecord::Base
  belongs_to :user
  belongs_to :item
  default_scope -> { order('created_at DESC') }
  validates :user_id, :presence => true
  validates :item_id, :presence => true
  validates_numericality_of :rating, :greater_than_or_equal_to => 0
  validates_numericality_of :rating, :less_than_or_equal_to => 5

end

class ItemsController < ApplicationController
  before_action :set_item, only: [:show]
  before_action :user_signed_in?, only: :create

  def create
    @item = Item.new
    @rating = @item.ratings.build
    @rating.comment = params[:item][:ratings_attributes][:comment]
    @rating.rating = params[:item][:ratings_attributes][:rating]
    @rating.user_id = current_user.id

    @item.name = params[:item][:name]
    @item.url = params[:item][:url]
    @item.full_address = params[:item][:full_address]
    @item.city = params[:item][:city]
    @item.country = params[:item][:country]
    @item.category = params[:item][:category]

    respond_to do |format|
      if @item.save
        #TODO create rating here (First rating of an Item)
        flash[:success] = "Welcome to inmyopnion"
        format.html { redirect_to @item, notice: 'Item was successfully created.' }
        format.json { render action: 'show', status: :created, location: @item }
      else
        format.html { render action: 'new' }
        format.json { render json: @item.errors, status: :unprocessable_entity }
      end
    end
  end

  def new
    @item = Item.new
  end

  def show
  end

  def destroy
  end

  private

  def set_item
    @item = Item.find(params[:id])
  end

  def item_params
    params.require(:item).permit(:name, :url, :full_address, :city, :country, :category, :ratings_attributes => [:rating, :comment])
  end

  def user_signed_in?
    #TODO: should display should sign in to rate an item
    redirect_to(root_url) unless signed_in?
  end
end
4

3 に答える 3

0

nested_attributesアソシエーションも検証する最終的に機能するプログラムのコードとドキュメントをいじってみましょう。これらは、以下にリストされている変更点です (** .... ** の間でマークされています)。

class Item < ActiveRecord::Base
  has_many :ratings, dependent: :destroy, **inverse_of: :item**
  accepts_nested_attributes_for :ratings, :allow_destroy => true
  validates :name , :length => { minimum: 3 }
  validates :category , :length => { minimum: 3 }
end

class Ratings < ActiveRecord::Base
  belongs_to :user
  belongs_to :item, **inverse_of: :ratings**
  default_scope -> { order('created_at DESC') }
  validates :user_id, :presence => true
  validates_presence_of :item
  validates_numericality_of :rating, :greater_than_or_equal_to => 0
  validates_numericality_of :rating, :less_than_or_equal_to => 5
end

@BroiSatseによって提案されているように、@item = Item.create(params[:item])まだギブスを与える ものを作成することはできません。また、そのドキュメントもそうではないはずですActiveModel::ForbiddenAttributesErrornested_attributes

問題は

class ItemsController < ApplicationController

def item_params
    params.require(:item).permit(:name, :url, :full_address, :city, :country, :category, :ratings_attributes => [:rating, :comment])
  end

それも解決するために取り組み、解決策が見つかったら回答を投稿します。

于 2013-08-12T08:40:21.883 に答える
0

以下の行にコメントすることで機能しました

class Ratings < ActiveRecord::Base
validates :item_id, :presence => true

しかし、私の関連付けは失敗し、なしでrspec testa を保存します。コードの残りの部分は、私が投稿したものと似ていますRatingsitem_id

@item = Item.create(params[:item])

与えるActiveModel::ForbiddenAttributesError

于 2013-08-12T07:52:49.883 に答える