Questions tagged «ruby-on-rails»

Ruby on Rails是一个用Ruby编写的开源全栈Web应用程序框架。它遵循流行的MVC框架模型,并以其“配置之上的约定”方法进行应用程序开发而闻名。

19
如何解决“您的Ruby版本是1.9.3,但是您的Gemfile指定了2.0.0”
我创建了一个Ruby项目,但在运行时bundle update,并bundle install返回一个错误: Your Ruby version is 1.9.3, but your Gemfile specified 2.0.0 它的图像是:http : //i.imgur.com/dZMhI11.png?1 我的gemfile是: ruby '2.0.0' #ruby-gemset=railstutorial_rails_4_0 gem 'rails', '4.0.0' group :development do gem 'sqlite3', '1.3.8' end gem 'sass-rails', '4.0.0' gem 'uglifier', '2.1.1' gem 'coffee-rails', '4.0.0' gem 'jquery-rails', '3.0.4' gem 'turbolinks', '1.1.1' gem 'jbuilder', '1.0.2' group :doc …

3
我应该在我的Gemfile中指定确切版本吗?
我注意到在rubygems.org上有很多宝石建议您使用主要版本而非确切版本来指定它们。例如... haml-rails宝石... gem "haml-rails", "~> 0.3.4" # "$ bundle install" will acquire the # latest version before 1.0. 但是,根据Bundler文档,在我看来,最好像这样精确地确定版本... gem "haml-rails", "0.3.4" 因此,这里有您的haml-rails gem,其所有依赖项都不会向前漂移。如果几周后在另一台计算机上签出项目并运行,$ bundle install您将拥有与指定的所有版本完全相同的版本。 我看到过分发布会破坏东西,我认为Bundler整个想法的一部分就是“ Bundle.lock”所有宝石版本。 但是在rubygems.org上,他们经常使用“〜>”,所以也许我遗漏了一些东西? 任何澄清对我理解邦德勒和宝石管理都将非常有帮助。

2
在Rails快捷方式中检查是否非零且不为空?
我为用户提供了一个显示页面,并且每个属性仅在该页面上可见,如果它不是nil且不是空字符串。在下面,我有我的控制器,@user.city != nil && @user.city != ""为每个变量编写同一行代码非常烦人。我对创建自己的方法不太熟悉,但是我可以以某种方式创建快捷方式来执行以下操作:@city = check_attr(@user.city)吗?还是有缩短此过程的更好方法? users_controller.rb def show @city = @user.city != nil && @user.city != "" @state = @user.state != nil && @user.state != "" @bio = @user.bio != nil && @user.bio != "" @contact = @user.contact != nil && @user.contact != "" @twitter = …


15
如何在Rails中停止Daemon服务器?
我正在使用以下命令运行我的Rails应用程序 $script/server -d webrick 在我的Ubuntu系统上,以上命令在后台运行webrick服务器。我可以使用kill命令杀死进程 $kill pid Rails是否提供任何命令来停止后台运行的守护程序服务器? 就像rails提供的启动服务器一样,谢谢。 编辑何时合适启动守护程序服务器?任何实时情况都会有所帮助,谢谢


7
如何基于当前的Rails环境设置回形针的存储机制?
我有一个Rails应用程序,其中有多个带有回形针附件的模型,这些模型都已上载到S3。此应用程序还具有经常运行的大型测试套件。这样做的缺点是每次测试运行都会将大量文件上载到我们的S3帐户,从而使测试套件运行缓慢。它还会减慢开发速度,并且需要您具有Internet连接才能处理代码。 是否有合理的方法基于Rails环境设置回形针存储机制?理想情况下,我们的测试和开发环境将使用本地文件系统存储,而生产环境将使用S3存储。 我还想将此逻辑提取到某种共享模块中,因为我们有几个需要此行为的模型。我想在每个模型中避免这样的解决方案: ### We don't want to do this in our models... if Rails.env.production? has_attached_file :image, :styles => {...}, :path => "images/:uuid_partition/:uuid/:style.:extension", :storage => :s3, :url => ':s3_authenticated_url', # generates an expiring url :s3_credentials => File.join(Rails.root, 'config', 's3.yml'), :s3_permissions => 'private', :s3_protocol => 'https' else has_attached_file :image, :styles => …

7
Rails-最佳实践:如何创建依赖的has_one关系
您能告诉我创建has_one关系的最佳实践是什么吗? fe,如果我有一个用户模型,并且必须有一个配置文件... 我该怎么办呢? 一种解决方案是: # user.rb class User << ActiveRecord::Base after_create :set_default_association def set_default_association self.create_profile end end 但这似乎并不干净……有什么建议吗?

3
rails-查找多个数组之间的交点
我试图找到多个数组之间的交集值。 例如 code1 = [1,2,3] code2 = [2,3,4] code3 = [0,2,6] 所以结果是2 我知道在PHP中可以使用array_intersect做到这一点 我希望能够轻松地添加其他数组,所以我真的不想使用多个循环 有任何想法吗 ? 谢谢亚历克斯

2
ActiveAdmin具有has_many问题;未定义的方法“ new_record?”
我正在尝试为与Step具有has_many关系的配方模型自定义ActiveAdmin表单。 class Recipe < ActiveRecord::Base has_many :steps end class Step < ActiveRecord::Base acts_as_list :scope => :recipe belongs_to :recipe end 我在ActiveAdmin文件中有以下与此相关的内容: form do |f| f.has_many :steps do |ing_f| ing_f.inputs end end 当我尝试加载表单时引发以下错误: 未定义的方法“ new_record?” 对于nil:NilClass 到目前为止,我已经将它隔离到has_many方法,但是我迷失了它。任何建议和帮助,将不胜感激!


9
Ruby on Rails:从test_unit切换到rspec
我正在阅读建议使用的教程rspec,但是我已经经历了许多默认的rails安装。我真的完全不需要重做安装。反正我跑步 $ rails g integration_test named 我懂了 invoke test_unit create test/integration/named_test.rb 当我运行时bundle,rspec列出了各种宝石,但test_unit没有列出。本教程似乎有Rails调用,rspec而不是test_unit不做任何其他事情。如何使Railsrspec与Integration Test Generator命令一起使用?

3
在Rails中request.remote_ip和request.ip有什么区别?
随着标题的发展,您可以使用两种方法获取客户端的ip。我想知道是否有任何区别。谢谢。 在源代码中 “ /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action _dispatch / http / request.rb” 257L,8741C def ip @ip ||= super end # Originating IP address, usually set by the RemoteIp middleware. def remote_ip @remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s end 但我真的不知道其中的含义。

4
Rails ActiveRecord如何在没有多个查询的情况下链接“ where”子句?
我是一名PHP开发人员,了解Ruby on Rails的精湛知识,我喜欢ActiveRecord,并且发现了一些非常有趣的东西,那就是ActiveRecord方法如何检测方法链的末端以执行查询。 @person = Person.where(name: 'Jason').where(age: 26) # In my humble imagination I'd think that each where() executes a database query # But in reality, it doesn't until the last method in the chain 此法术如何运作?

14
尝试从Rails控制器调用辅助方法时出现NoMethodError
NoMethodError尝试从我的一个控制器类访问在我的帮助器模块之一中定义的方法时遇到错误。我的Rails应用程序使用helper带有:all符号的class方法,如下所示: class ApplicationController < ActionController::Base helper :all . . end 我的理解是,这应该使我的所有控制器类都自动包含app / helpers目录中的所有帮助器模块,因此将所有方法混入控制器中。这个对吗? 如果我include在控制器中明确地建立了辅助模块,那么一切都会正常工作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.