ceth
ceth

Reputation: 45325

Cannot create new record in ActiveRecord

I am trying to migrate from Rails 3 to Rails 4. I have no idea why, but when I am trying to create new record I get the error message:

2 errors prohibited this group from being saved:
    Name can't be blank
    Tags can't be blank

Here is a controller code:

  def create
    @user = UserData.find_by_login(session[:cuser])
    @group = @user.groups.new(params[:group])


    puts "======================="
    p @user
    p params
    p params[:group]
    p @group
    p "========"
    p @user.groups

    respond_to do |format|
      if @group.save
        format.html { redirect_to(@group, :notice => 'Group was successfully created.') }
        format.xml  { render :xml => @group, :status => :created, :location => @group }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @group.errors, :status => :unprocessable_entity }
      end
    end
  end

Here is a console log:

=======================

As I see we have @user variable inizialized:

#<UserData id: 1, login: "demas", hashed_password: "e55105f41cd0391b440176a1cd5b14c2732410ca", salt: "xnqKXtOgwO", created_at: "2014-02-17 06:18:08", updated_at: "2014-02-17 06:18:08", last_login: "2014-02-16 20:00:00", login_count: 1, admin: 0>

I have group in the parameters:

{"utf8"=>"✓", "authenticity_token"=>"2qxvqJh0nrJiZt+7xvI0lZjA83Z+hw/GKzwrNlqEh44=", "group"=>{"name"=>"112", "tags"=>"2", "stop_tags"=>"", "download_on_refresh"=>"0", "site_id"=>"1"}, "commit"=>"Create Group", "action"=>"create", "controller"=>"groups"}
{"name"=>"112", "tags"=>"2", "stop_tags"=>"", "download_on_refresh"=>"0", "site_id"=>"1"}

But when I call @user.groups.new() I get empty group:

#<Group id: nil, name: nil, tags: nil, created_at: nil, updated_at: nil, user_data_id: 1, site_id: nil, stop_tags: nil, position: nil, last_sync: nil, download_on_refresh: nil>

I have checked @user.groups and as I see the relations works fine:

"========"
  Group Load (0.3ms)  SELECT "groups".* FROM "groups" WHERE "groups"."user_data_id" = ?  [["user_data_id", 1]]
#<ActiveRecord::Associations::CollectionProxy [#<Group id: nil, name: nil, tags: nil, created_at: nil, updated_at: nil, user_data_id: 1, site_id: nil, stop_tags: nil, position: nil, last_sync: nil, download_on_refresh: nil>]>
   (0.1ms)  begin transaction

Why @user.groups.new() don't create new record ?

Thanks in advance

Upvotes: 0

Views: 680

Answers (1)

Vageesh Bhasin
Vageesh Bhasin

Reputation: 563

Rails has implemented 'Strong parameters', hence directly using params[:group] to create a model will not work. You need to permit the controller to accept these parameter for model creation.

Your code:

def create
    @user = UserData.find_by_login(session[:cuser])
    @group = @user.groups.new(params[:group])  
    .
    .
end

Possible solution:

def create
    @user = UserData.find_by_login(session[:cuser])
    @group = @user.groups.new(group_post_params)
    .
    .
end

private
    def group_post_params
        params.require(:group).permit(:name, :tags, etc, etc)
    end

Upvotes: 3

Related Questions