覆盖Rails中的MIME类型

我想在Rails中覆盖JSON MIME类型(“application / json”)(“text / x-json”)。 我试图在mime_types.rb中再次注册MIME类型,但这不起作用。 有什么建议? 谢谢。

如何在Ruby中将字符引用转换为UTF-8字符串?

我有一些来自Feed的内容。 在这些提要中,UTF-8字符通常被编码为字符引用,即“å”是“å”。 为了避免在我的视图中对它们进行双重编码(即“å”),我想将它们转换回正常的UTF_8字符​​。 我怎么能在Ruby中做到这一点? 我想要: “å”。convert_to_utf8 =>“å”

查找嵌套开始和结束标记的最佳方法

我正在使用ROR制作一个基本的讨论板。 当用户发布对消息的响应时,输入文本区域使用标记预先填充引号中的消息: [QUOTE] 。 因此格式为: [QUOTE]引用的消息在这里[/ QUOTE] 目前,我有一个简单的解决方案,只要[QUOTE]或[/ QUOTE]仍然存在,使用message.sub('[QUOTE]’,’html go here’)用HTML替换[QUOTE]和[/ QUOTE]。 当我回复引用的消息时,我将HTML转换回[QUOTE]标签,以确保预先填充的输入textarea中没有HTML。 因此,引用的引用将如下所示: [QUOTE] [QUOTE]引用此处留言[/ QUOTE] [/ QUOTE] 这是问题所在。 如果我再次运行当前的方法,我将获得重复的HTML字段,如: quoted message goes here 相反,我希望能够有一个看起来像这样的解决方案: quoted message goes here 等等……关于循环这个的最佳方法的任何建议?

如何在Rails中访问POST变量?

我不是真正的Rails开发人员,我正在研究其他人的代码。 无论如何,我正在尝试更改登录function,以便它使用POST而不是GET。 现在它看起来像这样: def login email = params[:email]; password = params[:password]; # login logic … end 我需要更改什么才能使用POST变量? 编辑:在routes.rb中看起来相关的routes.rb match “service/login”, :to => “service#login” 如果我正在寻找其他东西,请告诉我。

使用Rails to_prepare事件

我正在尝试将to_prepare事件用于新的Rails 3.2.1项目。 我放置了以下内容: Rails.application.config.to_prepare do puts ‘here i am before a request’ end 进入config / initializers下的初始化器。 根据此处的文档,当在开发模式下运行时,此块应该在应用程序的每个请求上运行,并且仅在生产中运行一次。 我正在开发模式,这个块不会在每个请求上运行,而是仅在我启动应用程序时运行,而不是再次运行。 以下是我加载应用程序时的输出示例。 rails s => Booting WEBrick => Rails 3.2.1 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server here i am before a request [2012-03-02 20:29:46] INFO WEBrick 1.3.1 […]

使用Ruby中的“+”文件IO模式替换文件中的一行

Ruby初学者在这里! 我知道Ruby的File.open方法有某些模式,如r,w,a,r +,w +,a +和免费的b。 我完全理解r,w和模式的使用。 但我似乎无法理解如何使用带有“+”符号的那些。 任何人都可以向我提供一些链接,其中有示例以及使用它的解释吗? 是否可以用它来读取一行并用相同数量的内容编辑/替换它? 如果是这样,那怎么样? 示例数据文件:a.txt aaa bbb ccc ddd Demo.rb file = File.open “a.txt”,”r+” file.each do |line| line = line.chomp if(line==”bbb”)then file.puts “big” end end file.close 我试图用“大”替换“bbb”,但我得到了这个: – 在记事本++中 aaa bbb big ddd 在记事本中 aaa bbb bigddd

如何使用私人提交活动Feed?

我们如何为用户提供将活动设为私有的选项? 这将为用户提供他们想要的post的隐私权。 有人告诉我这段代码不起作用,因为它可能与“没有设置’私人’复选框以正常工作”有关 ,但私有复选框适用于隐藏公共配置文件上的提交 (只是不在活动源上) )。 class ActivitiesController < ApplicationController def index #Added .public @activities = Activity.visible.order("created_at desc").where(user_id: current_user.following_ids) end end class Activity { where(:hidden => false) } def visible? !hidden end end create_table “activities”, force: true do |t| t.boolean “hidden”, default: false t.integer “user_id” t.string “action” t.integer “trackable_id” t.string “trackable_type” t.datetime “created_at”, null: false […]

在ruby中使用散列排序的正确方法

我是ruby的新手,我正在尝试编写一个dijkstra函数,但我的哈希类似乎根本不起作用 def distance(start_code, end_code, map) #initialize hash for distance #distance are initialized to -1 dist_hash=Hash.new() start_hash=Hash.new() parent_hash=Hash.new() close_list=Array.new() find=-1 map.citylist.each do |e| dist_hash[e]=[+1.0/0.0] end start_hash[start_code]=0 parent_hash[start_code]=start_code while (start_hash.empty?)==false #sort the hash start_hash.sort_by {|k,v| v} puts ‘value’ puts start_hash.values() #pop the first item in the hash h=start_hash.shift() curr_key=h[0] curr_val=h[1] curr_city=map.findcity(curr_key) close_list<<curr_city.code #for every one in adjacent […]

用于查找注释的Ruby正则表达式?

我整天都在这里,我无法理解。 我在下面的字符串中有一些Ruby代码,并且只想匹配带有代码的行和代码的第一个注释(如果存在)。 # Some ignored comment. 1 + 1 # Simple math (this comment would be collected) # ignored # ignored user = User.new user.name = “Ryan” # Setting an attribute # Another ignored comment 这将捕获: “1 + 1” “Simple math” “user = User.new” nil “user.name = “Ryan” “Setting an attribute” 我正在使用/^\x20*(.+)\x20*(#\x20*.+\x20*){1}$/来匹配每一行,但它似乎不适用于所有代码。

这是Rack中的错误吗?

我正在尝试使用java客户端将多部分内容(文件和一些字符串)发布到localhost上的Sinatra服务器。 似乎服务器不喜欢POST消息。 堆栈跟踪是: ERROR NoMethodError: undefined method `rewind’ for “hi”:String D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/utils.rb:581:in`block in parse_multipart’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/utils.rb:499:in`loop’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/utils.rb:499:in`parse_multipart’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/request.rb:270:in `parse_multipart’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/request.rb:148:in `POST’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/methodoverride.rb:15:in `call’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1272:in `block in call’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1303:in `synchronize’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.2.6/lib/sinatra/base.rb:1272:in `call’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/content_length.rb:13:in `call’ D:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/handler/webrick.rb:52:in `service’ D:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service’ D:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run’ D:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread’ 我的服务器在post消息中打印出params。 以下是我的java客户端的内容: Content-Disposition: form-data; name=”file”; filename=”fff.jpg” Content-Type: image/jpeg Content-Transfer-Encoding: binary # Content-Disposition: form-data; name=”jjj” […]