2017-08-02 11 views
0

Ich versuche, diese json von jbuilderBindestrich in Schlüssel für json mit Jbuilder statt Strich

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { "match": { "source-id":2}} 
     ] 
    } 
    } 
} 

und mein Code ist hier zu machen

query = Jbuilder.encode do |json| 
        json.query do 
        json.bool do 
         json.must do 
         json.match do 
          json.source-id source.id 
         end 
         end 
        end 
        end 
       end 

Ich habe diese Fehlermeldung

syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' json.source-id source.id ^from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/console.rb:65:in start' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/console_helper.rb:9:in start' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:78:in console' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in run_command!' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/railties-5.0.2/lib/rails/commands.rb:18:in <top (required)>' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in require' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in block in require' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in load_dependency' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in require' from /Users/amir/source/innovate/self_driving_ideas/bin/rails:9:in ' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in load' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in block in load' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in load_dependency' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in load' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in call' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in call' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:in block in serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in fork' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in serve' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in block in run' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in loop' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in run' from /Users/amir/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in <top (required)>' from /Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require' from /Users/amir/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

Antwort

1

Das Problem ist mit Bindestrich in source-id Schlüsselname. Versuchen Sie dies, wenn source_id als Schlüssel funktioniert.

query = Jbuilder.encode do |json| 
       json.query do 
       json.bool do 
        json.must do 
        json.match do 
         json.source_id source.id 
        end 
        end 
       end 
       end 
      end 

UPDATE ::

Ansonsten können Sie dies der Schlüssel formatiert werden:

Jbuilder.encode do |json| 
    json.key_format! ->(key) { (key=="source_id") ? "source-id" : key} 
    json.query do 
    json.bool do 
     json.must do 
     json.match do 
      json.source_id source.id 
     end 
     end 
    end 
    end 
end 

alle Strings zu transformieren:

Jbuilder.encode do |json| 
    json.key_format! :dasherize 
    json.query do 
    json.bool do 
     json.must do 
     json.match do 
      json.source_id source.id 
     end 
     end 
    end 
    end 
end 

oder verwenden Sie set! Syntax wie json.set! "source-id", source.id

+0

nein ich will es nicht unterstreichen, es sollte Bindestrich sein – Mini

+0

du müsstest eine andere Methode benutzen, um Schlüssel zu formatieren, wenn es nur source_id ist, die du mit hypen willst dann hardcode es, wie ich es gezeigt habe. Ansonsten benutze 'gsub (" _ "," - ")' als Formatierer –

+0

der zweite hat einen Fehler ArgumentError: falsche Anzahl von Argumenten (gegeben 0, erwartet 1) – Mini

Verwandte Themen