A
A
Alexey Kolesnikov2015-10-18 12:13:00
Ruby on Rails
Alexey Kolesnikov, 2015-10-18 12:13:00

Can't assign a value to a Rails model field?

Friends I can’t understand at all what’s the matter, I’m just studying the rails, maybe the problem is on the surface ..
There is a model

class Liquid < ActiveRecord::Base
  attr_accessor :name, :chemical
end

and migration
class CreateLiquids < ActiveRecord::Migration
  def change
    create_table :liquids do |t|
      t.string :name
      t.string :chemical
      t.timestamps null: false
    end
  end
end

The migration is applied, in the rail console I create an instance of the x = Liquid.new class and set, for example, chemical - everything is set, and if I set a value for x.name, then x.name remains nil, what's the point?
>> x.name=1
#<Liquid id: 8, name: nil, chemical: "1", created_at: "2015-10-18 08:58:31", updated_at: "2015-10-18 08:58:31">

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
Roman Mirilaczvili, 2015-10-18
@2ord

This is an innovation in RoR version 4.
Read about strict parameters .

_
_ _, 2015-10-18
@AMar4enko

1. Rename the field
2. Walk through the debugger

A
Alexey Kolesnikov, 2015-10-18
@0xdeface

Created a migration renamed to nam - it worked, deleted the database file, migrations, did everything else
with the name field does not work
.. PS magic attr_accessor is not needed at all, who will have a similar problem

T
thepry, 2015-10-19
@thepry

Why is attr_accessor here?

E
Eugene, 2015-10-22
@epalguev

find in the controller in the "private" section a function called CONTROLLER NAME_params, see params.require(:CONTROLLER NAME).permit( _search here_ ) allowed params are listed in brackets, there should be ":name, :chemical", if not, then add . And remove "attr_accessor :name, :chemical" from the model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question