Creating an object with the results of the has_many association in an element cannot be empty

I have the following associations and the controller associated with them, in my form I add each field, as it should be. But I still get the error. Ratings element cannot be empty when I try to create an element. I am using Rails 4.0 . I searched a lot for this, but could not find what I am doing wrong. Thankyou!

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 
0
source share
3 answers

Simplify your controller! Since you allow nested attributes, this should be enough:

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

The problem may be caused by the fact that the @rating object is not saved.

+1
source

I earned by commenting the line below in

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

but my rspec test association crashes and saves Ratings without item_id . The rest of the code is similar to what I posted as

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

gives ActiveModel::ForbiddenAttributesError

0
source

Playing a lot with nested_attributes code and documents, finally with a working program that also checks the association. These are the changes (indicated between ** .... **) listed below

 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 

Still unable to create one of @item = Item.create(params[:item]) which still gives ActiveModel::ForbiddenAttributesError as suggested by @BroiSatse, as well as nested_attributes documents, which should not be in this case

the problem may be in

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

will work to resolve this and send a response if I find a solution.

0
source

Source: https://habr.com/ru/post/905441/


All Articles