小编Ale*_*pov的帖子

如何在ActiveAdmin gem中创建自定义页面

Ruby 2.0,Rails 4.0,PSQL 9.3

在ActiveAdmin文档中有一个示例:

ActiveAdmin.register_page "My Page" do
  content do
    para "Hello World"
  end
end
Run Code Online (Sandbox Code Playgroud)

我在哪里放这个代码?文件说:

在上面的示例中,将在/ admin/my_page创建一个新页面,标题为"我的页面",内容为"Hello World".

这意味着这样的文件会以某种方式自动创建?不过,我创建了一个名为import.rbapp/admin的文件,菜单中的Import项确实出现了.但是,我无法使用HTML,因为此文件是.rb而不是.erb.我想,为了能够使用html,我需要在方法中创建一个partial和den渲染它content.但当我看到下面app/views没有admin文件夹(只layouts).这是否意味着我需要创建该文件夹adminapp/views?如果是的话,我应该把我的部分放在哪里 - 直接放在app/views/admin新文件夹下面或下面app/views/admin/import

我很抱歉菜单问题,但ActiveAdmin的文档非常适中.如果有人能够提供有关在ActiveAdmin中为新页面创建和添加内容所需步骤的详细说明,我将不胜感激.

ruby-on-rails activeadmin ruby-on-rails-4

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

Ruby中的Enumerable#each和Enumerable#each_entry有区别吗?

可枚举文件没有明确说明这each是一个别名each_entry,但在描述each_entry比赛正是我所期待的each.

在两个答案的示例中,定义了新类,它们实现Enumerable模块并定义each方法.

有人可以给出一个内置类的例子,比如Array或者Hash,它的行为方式与each和不同each_entry

ruby syntax each

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

如果一个元素是可点击的,如何在Capybara中测试?

如果在页面上输入文本框可见且可点击,我需要在Capybara中进行测试.我知道如何测试可见性,但我找不到像.clickable这种精神的方法.如何在Capybara做到这一点?

testing ruby-on-rails capybara

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

如何根据属于第一个模型的另一个模型的属性查询模型?

如果我有一个模型Person,has_many Vehicles每个模型都Vehicle可以是类型,car或者motorcycle我如何查询所有拥有汽车的人和所有拥有摩托车的人?

我不认为这些是正确的:

Person.joins(:vehicles).where(vehicle_type: 'auto')
Person.joins(:vehicles).where(vehicle_type: 'motorcycle')
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails active-record-query

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

如何在RSpec中包含几个模块?

我不确定将几个模块包含在RSpec中的方法,所以让我来描述一下我的情况.

app/helpers我有两个文件与助手,包含模块ApplicationHelperMailersHelper.虽然这些是我在视图和邮件中使用的视图助手,但我也在我的测试中使用了一些方法,所以它们必须在describe子句中可访问.

app/spec/mailers我下面还有一个包含模块的文件Helpers.该模块包含仅在测试中使用的方法(主要是长期望的包装方法).

另外,我有以下代码:

class Helpers
  include Singleton
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::NumberHelper
end

def helper
  Helper.instance
end
Run Code Online (Sandbox Code Playgroud)

它的目的是使测试中的视图助手可用.目前,此代码位于Helper包装器方法之前的模块中,因为它们使用该Helper.instance方法.

我有三个邮件,我想测试.那么如何在测试中使所有这些东西都可以访问?

选项1

在每个邮件程序规范中直接使用include,如下所示:

require 'spec_helper'

describe MyMailer do
  include ApplicationHelper
  include MailersHelper
  include Helpers
  ...
end
Run Code Online (Sandbox Code Playgroud)

选项2

在文件中helpers.rb我这样做:

require 'spec_helper'

module Helpers
  include MailersHelper
  include ApplicationHelper
  ...

end

RSpec.configure { |c| c.include Helpers }
Run Code Online (Sandbox Code Playgroud)

并在我使用的每个邮件规范中 require_relative './helpers'

选项3

和上面一样但不是包括ApplicationHelperMailersHelperHelpers …

ruby rspec module ruby-on-rails code-organization

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

Bundler找不到gem"activesupport"和"railties"的兼容版本

我在Win 8 PC上成功安装了Railsinstaller并启动了一个项目.我设置了github并将项目推送到它.在第一台电脑上,一切都在找工作.然后我在第二台Win 8 PC上安装了Railsinstaller.在第二台电脑上,我跑了gem update rails.然后我跑了git clone [url],那也很成功.但我无法启动rails服务器,因为缺少gem.这是输出gem list:

actionmailer (4.0.0, 3.2.13)
actionpack (4.0.0, 4.0.0.rc2, 3.2.13)
activemodel (4.0.0, 3.2.13)
activerecord (4.0.0, 3.2.13)
activerecord-deprecated_finders (1.0.3)
activerecord-sqlserver-adapter (3.2.10)
activeresource (3.2.13)
activesupport (4.0.0, 4.0.0.rc2, 3.2.13)
arel (4.0.0, 3.0.2)
atomic (1.1.10)
bcrypt-ruby (3.1.1 x86-mingw32)
bigdecimal (1.1.0)
builder (3.1.4, 3.0.4)
bundler (1.3.4)
coffee-rails (4.0.0, 3.2.2)
coffee-script (2.2.0)
coffee-script-source (1.6.1)
devise (3.0.0)
diff-lcs (1.2.4)
erubis (2.7.0)
execjs (1.4.0)
hike (1.2.1)
i18n (0.6.4, 0.6.1)
io-console (0.3)
journey (1.0.4)
jquery-rails (3.0.4, …
Run Code Online (Sandbox Code Playgroud)

rubygems ruby-on-rails

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

无法从Rails 4和PSQL 9.3中的表中删除索引

在我的schema.rb中,我有以下行:

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
Run Code Online (Sandbox Code Playgroud)

当我\di在psql中运行时,我得到:

Schema |                             Name                             | Type  | Owner |         Table
--------+--------------------------------------------------------------+-------+-------+-----------------------
 public | index_users_on_email                                         | index | alex  | users
Run Code Online (Sandbox Code Playgroud)

但是,如果我在迁移中包含以下内容之一:

  • remove_index:users,name :: index_users_on_email
  • remove_index:users,column :: email
  • remove_index:users,:email
  • 执行'DROP INDEX index_users_on_email'

我收到以下错误:

rake aborted!
An error has occurred, this and all later migrations canceled:

Index name 'index_users_on_email' on table 'users' does not exist
Run Code Online (Sandbox Code Playgroud)

我也发现了这个问题.那么任何想法?

ruby migration ruby-on-rails database-indexes rails-postgresql

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

如何在Ruby中实现goto和label方法?

作为主题DSL的作业,我需要在Ruby中编写内联汇编程序.我知道笑话在我们身上:Ruby 1.9如何支持Goto声明,但我不能使用它.这是一个非常简单的实现与具有汇编四个寄存器- ,ax,bx,cx,dx拿着整数值,关于这一点我可以做一些操作,如设置其值为(mov),比较两个寄存器(cmp),增加一个寄存器(inc),跳转到一个特定的地方(jmp)和其他一些种类.界面将是这样的:

Asm.asm do
  mov cx, 1
  jmp l1
  mov ax, 1
  label l1
  mov dx, 1
end
Run Code Online (Sandbox Code Playgroud)

jmp方法将接受标签名称或其他功能之一的序列号.所以我的问题是:在块中:

{
mov cx, 1
jmp l1
mov ax, 1
label l1
mov dx, 1
}
Run Code Online (Sandbox Code Playgroud)

如何跟踪当前的功能数量.我的实现看起来大致如下:

module Asm
  def self.asm(&block)
    memory = Memory.new
    memory.instance_eval(&block)
    memory.table.values
  end

  class Memory
    attr_reader :table

    def initialize
      @table = { ax: 0, bx: 0, cx: …
Run Code Online (Sandbox Code Playgroud)

ruby goto

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

复选框选中的属性在Chrome或Firefox开发者工具中不会更改

假设您有一个复选框,该复选框已选中.在Chrome或Firefox中,单击inspect元素时,在HTML中您会看到:

<input checked="checked" class="some-class" id="some-id" name="some-name" type="checkbox" value="some-value">
Run Code Online (Sandbox Code Playgroud)

我希望,当我点击复选框并取消选中它时,html将会改变,checked属性将消失:

<input class="some-class" id="some-id" name="some-name" type="checkbox" value="some-value">
Run Code Online (Sandbox Code Playgroud)

但事实并非如此.为什么?

html checkbox firebug google-chrome-devtools

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

如何使用ActiveRecord仅选择存储在Postgres中的json的一部分

假设我有一个模型User,它有一个json叫做的字段settings.我们假设这个字段大致如下:

{
  color: 'red', 
  language: 'English', 
  subitems: 
    {
      item1: true, 
      item2: 43, 
      item3: ['foo', 'bar', 'baz']
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我这样做,User.select(:settings)我将获得每个用户的所有设置.但我想只为用户提供语言.我试过两个:

User.select("settings -> 'language'")
Run Code Online (Sandbox Code Playgroud)

User.select("settings ->> 'language'")
Run Code Online (Sandbox Code Playgroud)

但这只是返回空对象:

[#<User:0x007f381fa92208 id: nil>,
...]
Run Code Online (Sandbox Code Playgroud)

这是可能吗?如果是 - 我可以使用json或者是否需要切换到jsonb

postgresql activerecord json ruby-on-rails jsonb

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