0

我有以下关联和相关控制器,在我的表单中,我添加了每个字段。但是当我尝试创建一个项目时,我仍然得到一个错误评级项目不能为空。我正在使用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

1

简化您的控制器!由于您允许 nested_attributes 这应该足够了:

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

该问题可能是由未保存 @rating 对象引起的。

于 2013-08-10T15:22:15.130 回答
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 test失败并保存了一个Ratings没有item_id. 其余代码与我发布的类似

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

ActiveModel::ForbiddenAttributesError

于 2013-08-12T07:52:49.883 回答