小编Ala*_*ain的帖子

ElasticSearch&Tire:使用Mapping和to_indexed_json

在阅读轮胎文档时,我的印象是你应该使用其中之一mappingto_indexed_json方法,因为(我的理解是......)mapping用来喂养to_indexed_json.

问题是,我找到了一些使用它们的教程.为什么?

基本上,我的应用程序现在正在使用to_indexed_json但我无法弄清楚如何设置某些属性的提升值(因此我开始查看映射的原因),我想知道是否使用两者会产生一些冲突.

ruby ruby-on-rails elasticsearch tire

20
推荐指数
1
解决办法
5295
查看次数

如何在lib/class中使用content_tag

我的问题:我想在我的lib文件夹中创建一个类TitlePanel,该类使用content_tag方法,但我无法弄清楚如何加载它.我已经尝试了所有我想到的'xxx'并且它一直给我错误消息,它无法找到所需的文件.

基本上,我想要做的是创建一个生成html的帮助器,但我必须先通过一个类来存储一些值.我想要做的事情:

title = TitlePanel.new("this is my title")
title.add_panel "help" do
  content_tag :div, "this is the help section..."
end
title.add_panel "search" do
   content_tag :div, "this is the search section..."
end

title.to_s
Run Code Online (Sandbox Code Playgroud)

输出是使这项工作所需的所有HTML.

ruby-on-rails

12
推荐指数
2
解决办法
5424
查看次数

jQuery ajax:即使响应正常,错误也会运行200

我有一个表单,通过AJAX提交表单:remote => true.查看服务器日志和FireBug,我得到200 OK的响应,它以下列形式返回JSON:

{ "email": "test@test.com"}
Run Code Online (Sandbox Code Playgroud)

然后我有这两个处理程序:

$('#new_invitation').bind("ajax:success", function(event, data, status, xhr) {
    alert('test');
});

$('#new_invitation').bind("ajax:error", function() {
    alert('error');
});
Run Code Online (Sandbox Code Playgroud)

即使我回到200OK,它也会触发错误处理程序.我设法让成功处理程序工作的唯一一次是我在标题中发送一个200的空响应.

我无法弄清楚为什么这不起作用:-S

编辑1 ------------完成这些更改后:

$('#new_invitation').bind("ajaxSuccess", function(event, data, status, xhr) {
    alert('test');
});

$('#new_invitation').bind("ajaxError", function(jqXHR, textStatus, errorThrown) {
alert('error');
    console.log(jqXHR.responseText);
    console.log(textStatus.responseText);
    console.log(errorThrown.responseText);
});
Run Code Online (Sandbox Code Playgroud)

我仍然得到同样的错误.日志的东西给了我:

undefined
my_email@test.com
undefined
Run Code Online (Sandbox Code Playgroud)

这是表单的代码(标准Rails的东西):

<%= form_for @shoot.invitations.new, :url=>shoot_invitations_path(@shoot), :remote => true, :html => {:class => 'form-inline'} do |f| %>
    <%= f.text_field :email, :'placeholder' => 'ex: test@test.com' %>
    <%= f.text_field :role, :'placeholder' => 'ex: Photographer' %>
    <%= …
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery ruby-on-rails-3

10
推荐指数
2
解决办法
9403
查看次数

为什么Prawn会自动调整图像大小?

当我尝试使用Prawn插入图像(PNG为72dpi)时,如下所示:

image "#{Rails.root}/public/images/pdf/logo.png"
Run Code Online (Sandbox Code Playgroud)

Prawn插入图像但是将其缩放约30%,这使得它模糊(并且更大.)我在文件中没有任何其他东西,PDF初始化是相当标准的:

super(:page_layout => :portrait,
      :left_margin => 0.5.in,
      :right_margin => 0.5.in,
      :top_margin => 1.in,
      :bottom_margin => 0.5.in)
Run Code Online (Sandbox Code Playgroud)

根据文档,Prawn应该将图片插入文档而不做任何修改.为什么这张图片会重新调整大小?


注意:我注意到边界框也比它们应该更大(与我的图像的比例相同).

pdf ruby-on-rails prawn

7
推荐指数
1
解决办法
3791
查看次数

设计忽略我的自定义策略

我想创建一个用于访问API的自定义身份验证策略.我按照Devise中的示例代码忽略了自定义策略.

问题是有效吗?我的Api策略中的方法永远不会运行(基于试图撬开它).

我的代码:

module Devise
  module Strategies
    class Api < Devise::Strategies::Base
      def valid?
        binding.pry
        params[:request_source] == 'api'
      end

      def authenticate!
        #do stuff here

        if user
          success!(user)
        else
          warden.custom_failure!
          render :json=> {:success=>false, :message=>"Error with your login or password"}, :status=>401
        end
      end
    end

    Warden::Strategies.add(:api, Devise::Strategies::Api)

  end
end
Run Code Online (Sandbox Code Playgroud)

并在设计初始化程序中:

config.warden do |manager|
  manager.default_strategies.unshift :api
end
Run Code Online (Sandbox Code Playgroud)

无论我做什么,似乎Devise总是使用其默认策略.AFAIK,这应该够了......

- - - -编辑 - - - -

我在我的设计初始化程序的顶部需要这样的策略:

require Rails.root.join('app/devise/strategies/api')
Run Code Online (Sandbox Code Playgroud)

我知道策略是在启动时加载的,因为如果我在类中放入一个pry调用,它将启动一个pry会话.但是方法内部的Pry调用永远不会运行.:-S

ruby-on-rails devise warden

6
推荐指数
1
解决办法
2430
查看次数

我的复杂路线的控制器规格中没有路线匹配

我有这条路线:

resources :items, path: 'feed', only: [:index], defaults: { variant: :feed }
Run Code Online (Sandbox Code Playgroud)

它嵌套在api和v1名称空间中.(request_source参数来自Api命名空间).

我想在我的控制器规范中测试索引操作.我试过了:

get :feed, community_id: community.id, :request_source=>"api"
Run Code Online (Sandbox Code Playgroud)

不起作用,也是如此:

get :index, community_id: community.id, :request_source=>"api", variant: 'feed'
Run Code Online (Sandbox Code Playgroud)

他说:

ActionController::RoutingError:
   No route matches {:community_id=>"14", :request_source=>"api", :variant=>"feed", :controller=>"api/v1/items"}
Run Code Online (Sandbox Code Playgroud)

- - - -编辑 - - - - -

我想使用变量将参数发送到控制器的原因是因为我有所有这些路径:

    resources :items, path: 'feed',    only: [:index], defaults: { variant: 'feed' }
    resources :items, path: 'popular', only: [:index], defaults: { variant: 'popular' }
Run Code Online (Sandbox Code Playgroud)

然后,在ItemsController中,我有一个前置过滤器"get_items"用于索引操作:

def get_items
  if params[:variant] == 'feed'
     ....
elsif params[:variant] == 'popular' …
Run Code Online (Sandbox Code Playgroud)

controller rspec routes ruby-on-rails

5
推荐指数
1
解决办法
768
查看次数

PG :: UndefinedObject:错误:类型"hstore"不存在但它确实存在

首先,这可能看起来像是重复:

postgres hstore存在且不存在于同一时间

但事实并非如此.虽然我在这种情况下收到相同的错误消息.在检查数据库中是否安装了hstore时,我们可以看到它是:

./psql -d photographerio_development -c '\dx'
                       List of installed extensions
  Name   | Version |   Schema   |                   Description                    
---------+---------+------------+--------------------------------------------------
 hstore  | 1.2     | hstore     | data type for storing sets of (key, value) pairs
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
Run Code Online (Sandbox Code Playgroud)

它也在template_1 DB上.

因此,当我尝试运行迁移以添加hstore时,我得到了PG::Error: ERROR: extension "hstore" already exists,当我注释掉这个迁移时,在下一个需要hstore的情况下,它说PG::UndefinedObject: ERROR: type "hstore" does not exist这有点悖论.

它是一个带有postgresql 9的Rails 4.0.1应用程序,我让hstore处理在这台机器上运行的其他一些项目.

postgresql ruby-on-rails hstore

5
推荐指数
2
解决办法
7034
查看次数

Memcached总是想念(rails)

嗨Stackoverflowers,我在这个问题上花了太多时间来保持理智,所以是时候寻求帮助了...

我有一个使用此方法的类:

def telecom_info

    Rails.cache.fetch("telecom_info_for_#{ref_num}", :expires_in=> 3.hours) do
      info = Hash.new(0)
      Telecom::SERVICES.each do |source|
          results = TelecomUsage.find(:all, 
                                      :joins=>[:telecom_invoice=>{ :person=> :org_person}], 
                                      :conditions=>"dotted_ids like '%#{ref_num}%' and telecom_usages.ruby_type = '#{source}'", 
                                      :select=>"avg(charge) #{source.upcase}_AVG_CHARGE,
                                                max(charge) #{source.upcase}_MAX_CHARGE,
                                                min(charge) #{source.upcase}_MIN_CHARGE,
                                                sum(charge) #{source.upcase}_CHARGE,

                                                avg(volume) #{source.upcase}_AVG_VOLUME,
                                                max(volume) #{source.upcase}_MAX_VOLUME,
                                                min(volume) #{source.upcase}_MIN_VOLUME,
                                                sum(volume) #{source.upcase}_VOLUME
                                                ")

          results = results.first
          ['charge', 'volume'].each do |source_type|                                        
            info["#{source}_#{source_type}".to_sym] = results.send("#{source}_#{source_type}".downcase).to_i
            info["#{source}_min_#{source_type}".to_sym] = results.send("#{source}_min_#{source_type}".downcase).to_i
            info["#{source}_max_#{source_type}".to_sym] = results.send("#{source}_max_#{source_type}".downcase).to_i
            info["#{source}_avg_#{source_type}".to_sym] = results.send("#{source}_avg_#{source_type}".downcase).to_i
          end
      end

      return info
    end
end
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,这是一个昂贵的调用,每个请求都会调用ALOT,因此我想缓存它.问题是memcached似乎不起作用,在日志文件中,我得到:

缓存读取:telecom_info_for_60000000

缓存未命中:telecom_info_for_60000000({})

奇怪的是,我知道memcached正在工作,因为它确实缓存了我在另一个模型中的其他一些函数的结果.

有什么建议?我在REE 1.8.7上运行Rails 2.3.5

ruby memcached ruby-on-rails

4
推荐指数
1
解决办法
571
查看次数

如何停用contentEditable

我有一个网页img.每次用户点击时img,它都会生成一个属性设置为的span标签.contentEditabletrue

我的问题是,如果用户点击img添加a span,点击span(从而激活可编辑区域),然后点击返回img,它将添加另一个可编辑span(所需效果),但它也将保留第一个span处于编辑模式.

如何将可编辑更改span为正常span

jquery contenteditable

3
推荐指数
1
解决办法
4169
查看次数