Simple syntax error infuriating me - Ruby on Rails

I am following the Blog app from the RoR download site. The tutorial says enter this code to get confirmation:

class Post < ActiveRecord::Base
  validates :name,  :presence => true
  validates :title, :presence => true,
                    :length => { :minimum => 5 }
end

I have this in my copy:

class Post < ActiveRecord::Base 
validates :name,  :presence => true,
validates :title,  :presence => true,
         :length =>  {  :minimum => 5  }

end

Which, as far as I can see, is correct, however I get these error messages when the page starts up:

c:/Sites/blog/app/models/post.rb:3: syntax error, unexpected tSYMBEG, expecting kDO or '{' or '('
validates :Title,  :presence => true,
           ^
C:/Sites/blog/app/models/post.rb:3: Can't assign to true
C:/Sites/blog/app/models/post.rb:4: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
         :length =>  {  :minimum => 5  }

Can anyone point out what I did wrong? It seems to me that the same thing.

+3
source share
5 answers

You have a comma at the end validates :name, :presence => truein your file post.rb; delete it.

+10
source
validates :name,  :presence => true,

it should be:

validates :name,  :presence => true

(note that the comma is removed at the end)

+6
source

, , - , .

, :

  class Post < ActiveRecord::Base
      validates:name, :presence=>true
      validates:title,:presence=>true,
               :length=>{:minimum=>5}   
  end
+2

validates :name, :presence => true.

0

I had the same error a while ago, and I put some attributes in the code so that it would be fixed.

Your code should look like this:

class Post < ActiveRecord::Base 
  attr_accessible :content, :name, :title
  validates name, :presence => true
  validates :title, :presence => true, length => { :minimum => 5 }
end

It worked for me.

0
source

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


All Articles