Tag: ruby

jQuery:如何更改.ready()文件的标题?

我在Ruby on Rails中使用了一些嵌套的布局,在其中一个布局中,我需要从div中读取string,并将其设置为文档的标题。 什么是正确的方式(如果有的话)来设置文档的标题? <script type="text/javascript"> $(document).ready(function() { // ??? }); </script>

Rails 3.0中的frorror_messages

Rails 3.0弃用f.error_messages ,现在需要一个插件来正常工作 – 但我想学习如何显示错误消息的(新)本地方式。 我正在遵循入门指南 ,该指南在实现注释表单时使用了弃用的方法。 例如: <h2>Add a comment:</h2> <%= form_for([@post, @post.comments.build]) do |f| %> <%= f.error_messages %> <div class="field"> <% f.label :commenter %><br /> <%= f.text_field :commenter %> </div> <div class="field"> <%= f.label :body %><br /> <%= f.text_area :body %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> 这是正确的方法(由脚手架产生): <%= […]

Rails检测请求是否是AJAX

在我的行动中,我只想回应处理,如果它从AJAX请求调用。 我如何检查? 我想要做这样的事情: def action @model = Model.find(params[:id]) respond_to do |format| if (wasAJAXRequest()) #How do I do this? format.html #action.html.erb else format.html {redirect_to root_url} end end

如何从Rails控制台使用Deviselogin用户?

加载Rails控制台后,我应该如何login一个用户? devise提供了一个testing助手,可以在testing中使用,我试图在控制台中使用: >> include Devise::TestHelpers >> helper.sign_in(User.first) 但是我得到: NoMethodError: undefined method `env' for nil:NilClass 无论如何,我想用真正的devise助手,而不是这个testing助手。 有没有办法做到这一点?

了解Ruby的加载path

我有点困惑,为什么我的项目无法加载所需的文件,这是一个非常简单的项目树: processor/ bin/ lib/ processor.rb processor/ mapper.rb reducer.rb 和我的processor.rb文件看起来像 require 'processor/mapper' require 'processor/reducer' class Processor end 而只是为了testing它的文件映射器看起来像: class Mapper def run puts "running map" end end 但是运行ruby lib/processor.rb导致: <internal:lib/rubygems/custom_require>:29:in `require': no such file to load — processor/mapper (LoadError) from <internal:lib/rubygems/custom_require>:29:in `require' from lib/processor.rb:3:in `<class:Processor>' from lib/processor.rb:2:in `<main>'

内部方法有可能吗?

我有一个方法里面的方法。 内部方法取决于正在运行的variables循环。 这是一个坏主意吗?

你推荐什么工具来configurationRails应用程序?

我一直在寻找Rails的分析工具。 我目前正在玩和testingruby-prof和railsbench,但我有点挫折,需要调整和mangling的数量,然后工作。 虽然我不介意(很多)调整,但我想知道是否还有其他更简单易用的工具来分析Rails应用程序? 你推荐哪些工具?

Ruby发送JSON请求

如何在ruby中发送JSON请求? 我有一个JSON对象,但我不认为我可以做.send 。 我必须有JavaScript发送表单? 或者我可以在ruby中使用net / http类吗? 用标题 – 内容types= JSON和正文的JSON对象?

send()在Ruby中做什么?

有人可以告诉我什么 send("#{Model.find…}") 是,是吗?

保存从select导轨4.1枚举

我使用Rails 4.1中的枚举来跟踪葡萄酒的颜色。 Wine.rb class Wine < ActiveRecord::Base enum color: [:red, :white, :sparkling] end 在我看来,我产生了一个select,所以用户可以select具有某种颜色的葡萄酒 f.input :color, :as => :select, :collection => Wine.colors 这将生成以下HTML: <select id="wine_color" name="wine[color]"> <option value=""></option> <option value="0">red</option> <option value="1">white</option> <option value="2">sparkling</option> </select> 但是,提交表单后,我收到一个参数错误,指出'1' is not a valid color 。 我意识到这是因为color必须等于1而不是"1" 。 有没有办法强制Rails将颜色解释为整数而不是string?