小编And*_*rth的帖子

Twitter Bootstrap与will_paginate一起使用

我让will_paginate工作正常,但我正面临错误连接Twitter Bootstrap.我的单行will_paginate链接成为项目符号列表.

<% will_paginate @thing %>
Run Code Online (Sandbox Code Playgroud)

变为:

<%= will_paginate @thing, :renderer => BootstrapPagination::Rails %>
Run Code Online (Sandbox Code Playgroud)

或者我觉得更现代:

<%= will_paginate @thing, renderer: BootstrapPagination::Rails %>
Run Code Online (Sandbox Code Playgroud)

我得到了垂直项目符号列表,我试过包括=和 - 符号,但我迷路了.

ruby-on-rails will-paginate twitter-bootstrap

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

使用RSpec测试rake任务后清除数据库

我正在尝试使用rspec测试我的rake任务,它运行良好,但问题是以后没有删除记录。

我放入config.use_transactional_fixtures = true配置文件没有任何影响。在其他测试中,它运行良好。

这是我的代码:

require 'rspec'
require 'spec_helper'
require 'rake'

describe 'my_app rake tasks'  do
    describe 'rake_task_1' do
        before { MyApp::Application.load_tasks }

        it 'should test it!' do
            10.times { create(:record) }
            Rake::Task["rake_task_1"].invoke
            Record.count.should == 10
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

在我的rake任务中,我正在使用执行查询ActiveRecord::Base.connection.execute。我确定它在RSepc中具有SQL转换功能。

有任何想法吗?

ruby rspec ruby-on-rails factory-bot

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

如果我不需要实时功能,为什么要使用Node.js?

我正在考虑将Node.js用于快速,流星或风帆等框架(具有社交功能的目录,如共享,消息传递和上传媒体).我没有计划明确要求实时功能的任何功能,所以使用Node.js而不是Rails是否有意义?

Node.js有很多嗡嗡声,我很想使用它,这样我就不会落后了.

ruby-on-rails node.js express meteor sails.js

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

缺失:资源创建的id路由

rake routes创建资源之后:文章说我应该得到:

Prefix Verb      URI Pattern                  Controller#Action
articles  GET    /articles(.:format)          articles#index
          POST   /articles(.:format)          articles#create
new_article  GET /articles/new(.:format)      articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article  GET     /articles/:id(.:format)      articles#show
         PATCH   /articles/:id(.:format)      articles#update
         PUT     /articles/:id(.:format)      articles#update
         DELETE  /articles/:id(.:format)      articles#destroy
    root GET     /                            welcome#index
Run Code Online (Sandbox Code Playgroud)

但是我的路线总是缺少那些:id在其中.

Prefix Verb   URI Pattern              Controller#Action
articles POST     /articles(.:format)      articles#create
new_articles  GET /articles/new(.:format)  articles#new
edit_articles GET /articles/edit(.:format) articles#edit
          GET     /articles(.:format)      articles#show
          PATCH   /articles(.:format)      articles#update
          PUT     /articles(.:format)      articles#update
          DELETE  /articles(.:format)      articles#destroy
     root GET     /                        welcome#index
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

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

使用初始值作为键来散列数组,使用块来生成键值

说我有一个数组:

a = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)

我想创建一个散列,其中每个数组元素都是一个键,值是一些块的输出,每个块都调用这些元素作为块变量:

{ 1 => 4, 2 => 5, 3 => 6 }
Run Code Online (Sandbox Code Playgroud)

我可以使用mapto_h:

a.map{ |n| [n, n + 3] }.to_h

#=> {1=>4, 2=>5, 3=>6}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以做到这一点?例如,如果它被调用hash_with,我将能够写:

a.hash_with{|n| n + 3}

#=> {1=>4, 2=>5, 3=>6}
Run Code Online (Sandbox Code Playgroud)

ruby arrays hash

3
推荐指数
2
解决办法
63
查看次数

Rails 4.1.0和authlogic bcrypt问题

我用rvm ruby​​ 2.1.0运行最新的OSX.在bundle updateRails 4.1.0和authlogic 3.4.1之后,当我启动服务器时出现此错误:

authlogic/crypto_providers/bcrypt.rb:1:in `require': cannot load such file -- bcrypt (LoadError)
  authlogic-3.4.1/lib/authlogic/crypto_providers/bcrypt.rb:1:in `<top (required)>'
  authlogic-3.4.1/lib/authlogic.rb:60:in `require'
  authlogic-3.4.1/lib/authlogic.rb:60:in `block in <top (required)>'
  authlogic-3.4.1/lib/authlogic.rb:59:in `each'
  authlogic-3.4.1/lib/authlogic.rb:59:in `<top (required)>'
  bundler-1.5.2/lib/bundler/runtime.rb:76:in `require'
  bundler-1.5.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
  bundler-1.5.2/lib/bundler/runtime.rb:72:in `each'
  bundler-1.5.2/lib/bundler/runtime.rb:72:in `block in require'
  bundler-1.5.2/lib/bundler/runtime.rb:61:in `each'
  bundler-1.5.2/lib/bundler/runtime.rb:61:in `require'
  bundler-1.5.2/lib/bundler.rb:131:in `require'
  application.rb:7:in `<top (required)>'
  railties-4.1.0/lib/rails/commands/commands_tasks.rb:79:in `require'
  railties-4.1.0/lib/rails/commands/commands_tasks.rb:79:in `block in server'
  railties-4.1.0/lib/rails/commands/commands_tasks.rb:76:in `tap'
  railties-4.1.0/lib/rails/commands/commands_tasks.rb:76:in `server'
  railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
  railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails bcrypt authlogic

2
推荐指数
1
解决办法
1030
查看次数

带papertrail的默认排序版本

我在我的Rails 4应用程序中使用paper_trail gem,我想设置一个默认的排序顺序.papertrail没有模型,只有控制器和初始化器.

我在哪里可以买到它?我必须为它创建一个模型吗?

default_scope order('created_at DESC')
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails paper-trail-gem ruby-on-rails-4

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

Rails ActiveRecord WHERE EXISTS查询

我有一个SQL查询,该查询返回了我需要的内容,但是我无法以“ Rails方式”将其转换为活动记录查询。

我的SQL查询是:

SELECT * from trips 
WHERE trips.title LIKE "%Thailand%"
AND EXISTS (SELECT * from places WHERE places.trip_id = trips.id AND places.name LIKE "%Bangkok%")
AND EXISTS (SELECT * from places WHERE places.trip_id = trips.id AND places.name LIKE "%Phuket%")
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用Rails这样的事情:

@trips=Trip.where("trips.title LIKE ?", "%Thailand%")
@trips=@trips.includes(:places).where(("places.name LIKE ?","%Bangkok%").exists?) => true, ("places.name LIKE ?","%Phuket%").exists?) => true)
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用,我对尝试尝试感到困惑。

mysql ruby-on-rails

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

创建新的 ActiveRecord 对象时如何调用方法

我正在尝试将我用纯 Ruby 编写的算法集成到 Rails 应用程序中。我的 Ruby 项目的主类可能是一个资源。

当它在 Ruby 中初始化时,我立即在initialize方法中调用了一个函数:

def initialize(keyword)
  @keyword = keyword                    
  @sources = get_titles_of_sources                  
end
Run Code Online (Sandbox Code Playgroud)

在 Rails 中,当我创建新对象时,我通常没有或至少看到方法initialize

@user = User.new(attribute1: value1, attribute2: value2)
Run Code Online (Sandbox Code Playgroud)

但这种风格不允许我在创建新对象时自动调用方法。

ruby activerecord ruby-on-rails

2
推荐指数
1
解决办法
2184
查看次数

来自RSpec的意外结果改变了`destroy`对象

我正在尝试测试destroy我的模型:

   subject(:product){ FactoryGirl.create(:product)}

    it "destroys product" do
      expect{product.destroy}.to change(Product,:count).by(-1)
    end
Run Code Online (Sandbox Code Playgroud)

但它失败了.谁能指出我做错了什么?

ruby rspec ruby-on-rails factory-bot

2
推荐指数
1
解决办法
283
查看次数