Tag: 语法

将SQL语法转换为ActiveRecord查询的工具

有许多问题可以帮助将特定的SQL查询转换为ActiveRecord查询。 是否有一些帮助/指导[在线]工具自动进行转换?

rails插件没有安装

我正在尝试安装restful_authentication插件使用以下git … git://github.com/Satish/restful-authentication.git 我正在尝试使用以下命令安装… c:\ Users \ Admin \ Downloads \ _ rails_apps \ students> ruby script/plugin install git://github.com/Satish/restful-authentication.git 但它没有按预期安装,请帮助,谢谢。

ruby on rails使用&阻止sytax:

所以说我有一个问题模型和一个答案模型和问题has_many答案(这是一个多项选择题)。 假设问题是Question对象的集合。 为了收集所有答案,我可以这样做: questions.collect(&:answers) 两个问题: 这种语法到底意味着什么? 是否扩展到 questions.collect { |q| q.answers } 或者还有其他什么东西在这里? 有办法吗? questions.collect { |q| q.answers.shuffle } 使用相同的语法? collect(&:answers.shuffle) 没有这样做。 我似乎无法在网上的ruby块教程中找到这个并且搜索它不起作用(搜索引擎忽略“&:”)。 我在一些inheritance的代码中找到了它。 谢谢

Rails Routes – 斜杠字符与哈希字符

在URL和rails路由中,使用斜杠字符与井号(井号)字符有什么区别? 这些工作 get “/static_pages/about” get ‘about’, to: ‘static_pages#about’, as: :about 这些没有 get “/static_pages#about” get ‘about’, to: ‘static_pages/about’, as: :about get ‘about’, to: ‘/static_pages#about’, as: :about 什么代码控制这种行为,背后的深层原因是什么? 回答: (这两个人回答得非常好,我很难选择哪一个标记为接受的答案。我希望以一种可能有助于人们的不同方式表明我对答案的理解。) 使用/符号后,字符串将被识别为附加到基本URL的URL字符串。 所以’#’字符将被解释为url的一部分,而url不喜欢使用’#’字符。 在不使用/字符的情况下,第一个单词以某种方式被识别为控制器名称,您可以使用“#”和操作名称进行跟进。

更改主键问题Rails 4.0

我按照本指南介绍了如何更改主键: http://www.lshift.net/blog/2013/09/30/changing-the-primary-key-type-in​​-ruby-on-rails-models/comment-page-1 这是我的代码: class Pk2 < ActiveRecord::Migration def up remove_column :contracts, :id # remove existing primary key rename_column :contracts, :contractId, :id # rename existing UDID column execute "ALTER TABLE contracts ADD PRIMARY KEY (id);" end def down # Remove the UDID primary key. Note this would differ based on your database execute "ALTER TABLE contracts […]

使用erb和Ruby块语法来实现JSX集成

在我的app/assets/javascripts/components/navbar.js.jsx.erb我可以在我的组件的render函数中编写这样的东西(我正在使用gem react-rails ): class Navbar extends React.Component { render() { return(   ); } } 一切正常,除了link_to with block,这给了我一个语法错误: 语法错误,意外的keyword_end,期待’)’ 如果我删除该块一切正常。 任何的想法? PS:要使用Rails助手,我在config/initializers/assets.rb添加了这些行: # Include Rails helpers in the assets pipeline Rails.application.config.assets.configure do |env| env.context_class.class_eval do include ActionView::Helpers include Rails.application.routes.url_helpers end end

语法错误“语法错误,意外的输入结束,期望keyword_end(SyntaxError)”

由于语法错误,我无法运行Rspec user_spec.rb测试。 可能在2个不同的文件中有太多“结束”? 我在某些地方添加并删除了“结束”而没有成功。 语法错误“语法错误,意外的输入结束,期望keyword_end(SyntaxError)”require’pec_helper’ user_spec.rb describe User do before do @user = User.new(name: “Example User”, email: “user@example.com”, password: “foobar”, password_confirmation: “foobar”) end subject { @user } it { should respond_to(:name) } it { should respond_to(:email) } it { should respond_to(:password_digest) } it { should respond_to(:password) } it { should respond_to(:password_confirmation) } it { should […]

为什么Ruby中有时需要括号?

我最近在查看Rails文档中的一些Ruby代码时遇到了一个奇怪的问题。 Ruby允许您传递像这些示例的参数: redirect_to post_url(@post), alert: “Watch it, mister!” redirect_to({ action: ‘atom’ }, alert: “Something serious happened”) 但那第二个案子对我来说很奇怪。 看起来你应该能够像这样传递它: redirect_to { action: ‘atom’ }, alert: “Something serious happened” 无论有没有括号,它都有相同的含义。 但相反,你得到: syntax error, unexpected ‘:’, expecting ‘}’ action后提到结肠。 我不确定它为什么会在那里,以及为什么使用括号会改变它。

Haml语法:将一条线分成几行

我在我的rails项目中使用HAML作为我的html模板。 我想弄清楚是否有可能划分很长的行并使其成为几行: %a.open-service{href: ‘#’, data: { service_name: service.description, balance_type: “coinsurance”, total: service.a_total_billed – service.a_rejected – service.a_not_covered, discount: service} } 正如您所看到的,我只想拥有一个带有href和一些数据属性的锚点,并使其成为一行不会是一个漂亮的代码。 但是,当我像上面那样做时,我得到一个错误:“不平衡的括号。” 任何帮助?

为什么三元运算符不能使用重定向

我喜欢三元运算符。 它确实清理了代码外观,但我有一种情况,它在Rails控制器中无法正常运行。 我收到语法错误: unexpected tSYMBEG, expecting keyword_do or ‘{‘ or ‘(‘ 我一直都遇到这个问题,它开启了将下面的声明更改为三元运算符。 当我尝试将它与重定向语句结合使用时,它总会发生。 我不知道这个规则吗? if nexti==0 then redirect_to :back else redirect_to edit_playt_path(id: actform[‘playt_id’], i: nexti) end nexti==0 ? redirect_to :back : redirect_to edit_playt_path(id: actform[‘playt_id’], i: nexti)