草庐IT

Element-Is-In-the-Viewport-Viewpo

全部标签

ruby-on-rails - cucumber + capybara : Problem with a scenario that redirects the browser outside of my app

GivenIhavearailsappAndI'musingcucumberAndI'musingcapybaraAndIhaveanactionthatresultsinaredirect_to"http://some.other.domain.com/some_path"WhenItestthisactionThenthein-appportionofthetestworksfineButIseethiserror:Noroutematches"/some_path"with{:method=>:get}(ActionController::RoutingError)所以capyb

ruby-on-rails - slim 的模板 : Is it possible to put two elements on the same line?

我经常想嵌套元素,比如下面的导航:ullia(href="#")linkNamelia(href="#")linkNamelia(href="#")linkName是否可以将li和a放在同一行?像li>a这样的语法会很好。 最佳答案 我相信你可以做这样的事情ulli:ahref="#"Link1li:ahref="#"Link2参见内嵌标签:http://rdoc.info/gems/slim/file/README.md#Inline_tags 关于ruby-on-rails-slim

ruby - 正则表达式 "empty range in char class error"

我的代码中有一个正则表达式,用于匹配url的模式并抛出错误:/^(http|https):\/\/([\w-]+\.)+[\w-]+([\w-.\/?%&=]*)?$/错误是“字符类错误中的空范围”。我发现原因在([\w-.\/?%&=]*)?部分。Ruby似乎将\w-.中的-识别为范围运算符,而不是文字-。给dash加上escape后问题解决。但原来的正则表达式在我同事的机器上运行良好。我们使用相同版本的osx、rails和ruby:Ruby版本是ruby1.9.3p194,rails是3.1.6,osx是10.7.5。在我们将代码部署到我们的Heroku服务器之后,一切都运行良好。

ruby - #<Hash :0x3d3cef0> (NoMethodError) in ActiveSupport 3 的未定义方法 `to_json'

to_json是否被删除了? 最佳答案 尝试添加require"active_support/core_ext"require'active_support'不会自行将行为注入(inject)核心类。这样你就可以选择你想要的扩展。使用core_ext将您熟悉的扩展从rails转储到核心类中。 关于ruby-#(NoMethodError)inActiveSupport3的未定义方法`to_json',我们在StackOverflow上找到一个类似的问题: ht

ruby-on-rails - ruby rails : How would i stay on the same page if the post is not saved?

defcreate@addpost=Post.newparams[:data]if@addpost.saveflash[:notice]="Posthasbeensavedsuccessfully."redirect_toposts_pathelseflash[:notice]="Postcannotbesaved,pleaseenterinformation."endend如果帖子未保存,则会重定向到http://0.0.0.0:3000/posts,但我需要留在页面上,带有文本输入字段,以便用户可以输入数据。后模型classPosttruevalidates:content,:pr

ruby-on-rails - 修复 "ruby installation is missing psych"错误?

我正在运行Rubyruby​​1.9.3p125,但我不断收到此错误,这让我无法在Rails中进行任何类型的开发。Itseemsyourrubyinstallationismissingpsych(forYAMLoutput).Toeliminatethiswarning,pleaseinstalllibyamlandreinstallyourruby.我用Google搜索了一下,似乎尝试了所有方法,但似乎没有任何效果。我真的很想开始一些新项目,但似乎无法绕过这个障碍。非常感谢任何帮助! 最佳答案 我在使用rvm安装ruby​​1.

ruby-on-rails - rails : Validation in model vs migration

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:RubyonRails:Isitbettertovalidateinthemodelorthedatabase?我看到可以在Rails模型和迁移中添加相同的约束/验证。但是哪一个是最好的方法呢?在模型和数据库级别进行验证是否是一种好的做法(以及为什么)?或者他们在rails上一样?例如我们可以在模型和迁移中对名称进行相同的验证classUsertrue,:presence=>trueendclassCreateUsertrue,:null=>falseendendend

ruby - 导轨 : how to get a file extension/postfix based on the mime type

我的问题是,RubyonRails是否具有类似于以下的功能:file_content_type=MIME::Types.type_for(file).first.content_type这将返回特定mime类型的文件扩展名或后缀?所以如果我传入'image/jpeg'函数将返回'jpg'寻找一种比编写完成相同工作的case语句更简洁的编码方式。 最佳答案 Rack::Mime具有这种能力(Rack是Rails的依赖):require'rack/mime'Rack::Mime::MIME_TYPES.invert['image/jpe

ruby-on-rails - rails : validating a field is present only if another is present

我有一个模型,其中有两个字段在技术上可以为空。字段名称是:is_activated和:activated_at。:activated_at仅在:is_activated设置为true时才需要。如果:is_activated为false,则不需要存在。在Rails中将此验证直接设置到ActiveRecord中的合适方法是什么? 最佳答案 您可以使用Proc在:activated_at验证器上。validates:activated_at,:presence,if:Proc.new{|a|a.is_activated?}推荐阅读:htt

ruby - 如何避免 RSpec 中的 "Useless use of == in void context"?

在RSpec中,如果我有警告并且有x.should==42another_line_of_code然后我得到一个关于的警告warning:uselessuseof==invoidcontext还有什么我可以做的吗关闭警告将其更改为bitbucket=(x.should==42) 最佳答案 使用:x.shouldeq(42)或者:x.shouldbe==42或者移动x.should==42使其成为itblock中的最后一行。对于那些思考但是为什么?的人我完全是Ruby的菜鸟,但这是我的理解:警告来自Ruby,因为像x.should==