简体   繁体   English

ruby on rails 5 lambda语法错误,意外=>,期望')'

[英]ruby on rails 5 lambda syntax error, unexpected =>, expecting ')'

I have a migration file create_subject (code of which is below), a Subject class contains scopes using lambda syntax. 我有一个迁移文件create_subject (其代码如下), Subject类包含使用lambda语法的范围。 When I call Subject.visible , I get syntax errors. 当我调用Subject.visible ,出现语法错误。

class CreateSubjects < ActiveRecord::Migration[5.0]
  def up
    create_table :subjects do |t|
      t.string "name"
      t.integer "position"
      t.boolean "visible", :default=>false

      t.timestamps
    end
  end

  def down
    drop_table :subjects
  end
end

This is my Subject class 这是我的学科课

Console error log 控制台错误日志

irb(main):003:0> Subject.visible
SyntaxError: C:/Users/SS/Sites/simple_cms/app/models/subject.rb:3: syntax error, unexpected =>, expecting ')'
scope :visible, -> { where (:visible => true) }
                                   ^
C:/Users/SS/Sites/simple_cms/app/models/subject.rb:4: syntax error, unexpected =>, expecting ')'
scope :invisible, -> { where (:visible => false) }
                                     ^
C:/Users/SS/Sites/simple_cms/app/models/subject.rb:7: syntax error, unexpected '|'
scope :search, -> {|query| where (["name LIKE ?", "%#{query}%"]) }
                ^
C:/Users/SS/Sites/simple_cms/app/models/subject.rb:7: syntax error, unexpected ( arg, expecting keyword_do or '{' or '('
scope :search, -> {|query| where (["name LIKE ?", "%#{query}%"]) }

Do not put space before opening parenthesis when calling methods. 调用方法时,请勿在打开括号之前放置空格。 It should be like this 应该是这样

class Subject < ApplicationRecord
  scope :visible, -> { where(:visible => true) }
  scope :invisible, -> { where(:visible => false) }
  scope :sorted, -> { order("position ASC") }
  scope :search, ->(query) { where(["name LIKE ?", "%#{query}%"]) }
  # and so on ...
end

If you are going to put query in pipes |query| 如果要将查询放入管道|query| then use: 然后使用:

scope :search,lambda{|query| where(["name LIKE ?", "%#{query}%"]) }

instead of 代替

scope :search, ->(query) { where(["name LIKE ?", "%#{query}%"]) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails语法错误,意外的&#39;\\ n&#39;,期望&。 或::或&#39;[&#39;或&#39;。 - Ruby on Rails syntax error, unexpected '\n', expecting &. or :: or '[' or '.' Ruby语法错误,意外的&#39;=&#39;,预期为&#39;)&#39; - Ruby syntax error, unexpected '=', expecting ')' 语法错误ruby on rails意外出现&#39;:&#39;,调用render json时期望&#39;}&#39;: - Syntax Error ruby on rails unexpected ':', expecting '}' when calling render json: 语法错误,意外的&#39;}&#39;,期望&#39;:&#39;Rails - syntax error, unexpected '}', expecting ':' Rails 红宝石语法错误,意外的“:”,期望使用keyword_then - ruby syntax error, unexpected ':', expecting keyword_then 获取语​​法错误,意外&#39;:&#39;,期望Ruby中的&#39;=&#39; - Getting syntax error, unexpected ':', expecting '=' in Ruby Rails 4.2语法错误,意外&#39;:&#39;,期待=&gt; - Rails 4.2 syntax error, unexpected ':', expecting => Rails link_to语法错误:意外的&#39;,&#39;,预期为&#39;)&#39; - Rails link_to syntax error: unexpected ',', expecting ')' 否则会出现Rails语法错误,意外的&#39;;&#39;,期望的是&#39;:&#39; - if else rails syntax error, unexpected ';', expecting ':' Ruby On Rails 收到错误:语法错误、意外的关键字_确保、期望输入结束 - Ruby On Rails getting the error: syntax error, unexpected keyword_ensure, expecting end-of-input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM