Tag: 的Ruby on 轨道

让控制器发回没有Content-Type标头的响应

我在控制器内部设置了一个Rails 3代理方法,如果允许用户,则使用Nginx的X-Accel-Redirect从远程服务器提供特定的URI。 不幸的是,Rails总是发送某种Content-Type头,它优先于从上游服务器返回的头。 我已经尝试了各种方法“说服”响应,根本不包含任何Content-Type标题,但它们没有效果或引发exception。 响应主体当然是空的,因为它无论如何都被忽略了。 我试过的那些显而易见的没有用,因为价值仍然是“text / html; charset ……”: response.headers[‘Content-type’] = ” response.headers[‘Content-Type’] = nil response.headers[‘Content-Type’] = ” response.content_type = ” response.content_type = nil 当然,设置特定的内容类型,如“image / gif”按预期工作,但控制器无法确定将要传送的内容,与远程服务器不同。 发送没有该标题的响应的最佳(最干净?)方式是什么?

带参数的Rails helper_method

在视图中允许控制器方法作为帮助器时,如何传递参数? 在我的example_controller.rb中,我有 class ExampleController < ApplicationController helper_method :group_exists? private def group_exists?(gid): .. code to check if group exists end end 在视图中(我使用slim),我想传递一个参数 – if group_exists?(group.gid) | Exists – else | Does not exist 然而,它引发了一个错误说 wrong number of arguments (2 for 0..1)

从Ruby on Rails中的链接下载video

我正在开发一个使用liveclickervideo播放器的Ruby on Rails项目。 为了能够为我的webapp添加下载function,我已经实现了一个链接,可以下载video。 我想用link_to这样做,并在相应的控制器内调用一个方法来下载video。 任何人都可以帮我实现我的方法,以便能够从该URL下载video到我的本地机器? 我在控制器索引方法中有这个代码。 def index send_file ‘website’, :type=>”video/mp4″, :x_sendfile=>true end

在模型中的belongs_to中使用委托时,会出现“未初始化的常量”错误

我正在使用三个表实现一个模型的视图,其中一个表连接表。 以下是表格: 食谱: class Recipe true # validates :directions, :presence => true # has_and_belongs_to_many :ingradients has_many :ingredients_recipes has_many :ingredients, :through => :ingredients_recipes accepts_nested_attributes_for :ingredients_recipes, :allow_destroy => true end 成分: class Ingredient :ingredients_recipes end 它们之间的连接表: class IngredientsRecipe “ingredient_id” delegate :name, :to => :ingredients end 这似乎工作正常,当我创建一个新的配方 – 我可以编辑配方行,一切运行顺利。 当我创建一个视图以使用Ingredients表中的成分名称显示配方时,我在连接表中创建了一个委托。 但是,当我尝试在视图中使用委托时: 我收到以下错误: uninitialized constant IngredientsRecipe::Ingredients 当我删除行时,它运行没有错误。 我是否错误地定义了代表或者导致这种情况的原因是什么?

在轨道上的ruby中生成参数

我想在控制器中使用更新方法来获取特定格式的参数。 我获取参数的当前格式是 Parameters: {“utf8″=>”✓”, “authenticity_token”=>”temp”, “performance_areas”=>{“0″=>{“performance_area”=>”New item”}, “1”=>{“performance_area”=>”Delete Item”}, “commit”=>”Submit”} 这是在我的文本字段中生成的名称,0和1表示区域索引。 我想要一些额外的参数,格式如下: Parameters: {“utf8″=>”✓”, “authenticity_token”=>”temp”, “performance_areas”=>{“0″=>{id: 1, description: “This is a test”,”performance_area”=>”New item”}, “1”=>{id: 2, description: “This is a test2″,”performance_area”=>”Delete Item”}, “commit”=>”Submit”} 任何指针如何以我想要的方式获取参数。 我确实有performance_areas,其中包含id和description。 如果需要进一步澄清,请告诉我。 编辑:表单代码(只是粘贴长代码的关键部分) :update}, :method => :put) do %> “performance_areas[#{i}][performance_area]” %>

rails中三种开发模式的区别

铁轨中的三种模式有什么区别: – In development mode, Rails reloads models each time a browser sends in a request, so the model will always reflect the current database schema. 编辑我问的是其他差异。 我提到了一个我正在寻找其他差异列表…… !!

使用日期不正常的地方。

我似乎无法在我的两个日期中正确地进行日期搜索。 我在搜索模型中使用以下内容搜索created_at日期。 # ————————– Begin created at dates —————————- # Only one date is filled in: documents = documents.where(“documents.created_at >= ?”, from_date) if from_date.present? and not to_date.present? documents = documents.where(“documents.created_at = ?”, from_date,) if from_date.present? and to_date.present? documents = documents.where(“documents.created_at <= ?", to_date) if to_date.present? and from_date.present? 请注意,我尝试了几个to_date – 1.day的变体,例如to_date和to_date + 1.day 我有一个包含from_date和to_date字段的表单 如果我只输入日期,例如03/24/15,我会得到所有我期望的项目 2016-03-24 […]

如何使用ActiveRecord获取查询结果中的行和列?

有没有办法让ActiveRecord执行自定义SQL查询并让它返回一个数组数组,其中第一行是列名,后面的每一行是行数据? 我想执行类似的事情: connection.select_rows_with_headers “SELECT id, concat(first_name, ‘ ‘, last_name) as name, email FROM users” 让它回归: [[“id”,”name”,”email”],[“1″,”Bob Johnson”,”bob@example.com”],[“2″,”Joe Smith”,”joe@example.com”]] 这将允许我在HTML表中打印自定义查询的结果,如下所示: 请注意, select_all不起作用,因为每个哈希中的键都是无序的,因此您丢失了查询中指定的结果顺序。

Rails:distance_of_time_NOT_in_words

就像在, distance_of_time(Time.now, Time.tomorrow).days = 1或沿着这些线的东西? 如果没有,那么实现这一目标的好方法是什么? 我知道有“from_now”,但为什么不会有from_whenever?

如何使用Koala gem取消授权用户Facebook权限

我正在使用Koala来处理FB调用。 一切都工作正常,除了我无法弄清楚如何取消授权用户的FB权限。 等效的REST调用将是: DELETE /{user-id}/permissions/{permission-name} Koala wiki表示通过以下方式支持所有REST调用: @rest = Koala::Facebook::API.new(oauth_access_token) @rest.fql_query(my_fql_query) # convenience method @rest.fql_multiquery(fql_query_hash) # convenience method @rest.rest_call(“stream.publish”, arguments_hash) # generic version 但这并没有告诉我太多。 我更喜欢使用Koala,因为我启用了app secret安全性,为普通FB REST调用生成app_secret_proof是一个很大的麻烦。 考拉透明地处理它。