小编Eri*_*oss的帖子

使用active_model_serializers序列化深层嵌套关联

我正在使用Rails 4.2.1active_model_serializers 0.10.0.rc2

我是API的新手并选择active_model_serializers因为它似乎成为rails的标准(虽然我不反对使用RABL或其他串行器)

我遇到的问题是我似乎无法在多级关系中包含各种属性.比如我有:

项目

class ProjectSerializer < ActiveModel::Serializer
  attributes                      :id, 
                                  :name,
                                  :updated_at

  has_many                        :estimates, include_nested_associations: true

end
Run Code Online (Sandbox Code Playgroud)

估计

class EstimateSerializer < ActiveModel::Serializer
  attributes                      :id, 
                                  :name, 
                                  :release_version, 
                                  :exchange_rate, 
                                  :updated_at,

                                  :project_id, 
                                  :project_code_id, 
                                  :tax_type_id 

  belongs_to                      :project
  belongs_to                      :project_code
  belongs_to                      :tax_type

  has_many                        :proposals

end
Run Code Online (Sandbox Code Playgroud)

建议

class ProposalSerializer < ActiveModel::Serializer
  attributes                      :id, 
                                  :name, 
                                  :updated_at,

                                  :estimate_id

  belongs_to                      :estimate
end
Run Code Online (Sandbox Code Playgroud)

当我点击/projects/1上面产生的时候:

{
  "id": 1,
  "name": "123 Park Ave.",
  "updated_at": "2015-08-09T02:36:23.950Z",
  "estimates": [
    {
      "id": 1, …
Run Code Online (Sandbox Code Playgroud)

json ruby-on-rails active-model-serializers

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

检查属性是否存在并设置的最佳方法是什么?

我有一个共同的视图,列出了两个不同的模型.唯一的区别是,在设置link_to动作时,其中一个模型具有link属性而另一个模型没有.我想检查link属性是否存在,如果存在,请检查它是否已设置.我有以下工作,但我想知道是否有更好的方法.

%li
  - if @element.has_attribute?("link") && @element.link
    = link_to @element.title, @element.link
  - else
    = link_to @element.title, @element
Run Code Online (Sandbox Code Playgroud)

ruby haml ruby-on-rails

28
推荐指数
3
解决办法
3万
查看次数

允许管理员使用Devise添加用户

我正在努力使它只有管理员可以添加设计使用.我已经得到它主要工作但是现在当我以管理员身份登录并提交注册表单时,它将错误地踢回来:You are already signed in.

我试过按照这里的说明:http://wiki.summercode.com/rails_authentication_with_devise_and_cancan但似乎没有提到这种情况.

我是否需要进一步覆盖editors_controller才允许这样做?

这是我的路线("编辑"是我的用户模型的名称):

devise_for :admins, :skip => [:registrations]

as :admin do
  get 'admin/editors'        => 'editors#index',                  as: :admin_editors
  get 'admin/editors/new'    => 'editors#new',                    as: :new_editor
  delete 'admin/editors/:id' => 'editors#destroy',                as: :destroy_editor
end


devise_for :editors, :skip => [:registrations],  :controllers => { :registrations => "editors" }
Run Code Online (Sandbox Code Playgroud)

和我editors_controller的"app/controllers /"

    class EditorsController < Devise::RegistrationsController
  before_filter :check_permissions, :only => [:new, :create, :cancel]
  skip_before_filter :require_no_authentication

  def dashboard
    render "editors/dashboard.html.haml"
  end

  def index
    @editors = …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails devise

8
推荐指数
1
解决办法
8576
查看次数

Rails has_many:通过PG ::错误:错误:列引用"id"是模糊错误

我是rails的新手,我一直在试图获得两个has_many:虽然关系可以解决(而不是像本帖所解释的那样使用has_and_belongs_to_many http://blog.flatironschool.com/post/35346328762/why-you -dont-need-have-and-belongs-to-many)但现在遇到Postgres错误:

PG::Error: ERROR:  column reference "id" is ambiguous
LINE 1: ...on_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIM...
                                                             ^
: SELECT  1 AS one FROM "components" INNER JOIN "collection_components" ON "components"."id" = "collection_components"."component_id" WHERE "collection_components"."collection_id" IS NULL AND "components"."id" = 1 ORDER BY id ASC LIMIT 1
  Rendered collections/_form.html.haml (117.0ms)
  Rendered collections/new.html.haml within layouts/application (143.5ms)
Completed 500 Internal Server Error in 164ms

ActiveRecord::StatementInvalid - PG::Error: ERROR:  column reference "id" is ambiguous
LINE 1: …
Run Code Online (Sandbox Code Playgroud)

postgresql ruby-on-rails has-many-through

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

将自定义工具栏添加到rails中的CKEditor

我正在使用CKEditor和CKEditor gem(https://github.com/galetahub/ckeditor)并且一切正常,直到我尝试添加自定义工具栏.

我见过的一些帖子建议使用config.js文件.但是,根据说明进行设置,没有/ckeditor/config.js文件app/assets/javascripts.此外,如果我添加/ckeditor/config.js到javascripts目录,文件上载功能将停止工作.即使config.js是空文件也会发生这种情况.通过重新启动服务器,"上载"选项卡将变为隐藏且无法正常运行.

有没有办法可以普遍定制工具栏?或者即使我可以将选项内联或有用的东西配对......

使用Rails 3.2.11

在我的Gemfile中我有:

gem "jquery-rails", "~> 2.2.1"
gem "ckeditor"
gem "carrierwave"
gem "mini_magick"
gem "cloudinary"
Run Code Online (Sandbox Code Playgroud)

在application.rb我有:

config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
Run Code Online (Sandbox Code Playgroud)

在application.js我有:

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require ckeditor/init
//= require_tree ../../../vendor/assets/javascripts/.
//= require_tree .
Run Code Online (Sandbox Code Playgroud)

在我的表格中,我有:

= f.cktext_area :content
Run Code Online (Sandbox Code Playgroud)

我尝试使用的config.js文件:

CKEDITOR.editorConfig = function( config ) {
  config.toolbar_Custom = [
    { name: 'document',    items : [ 'Source','-','Save','NewPage','DocProps','Preview','-','Templates' ] },
    { name: 'clipboard',   items : …
Run Code Online (Sandbox Code Playgroud)

javascript ruby-on-rails ckeditor

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

自制软件未安装到/ usr/local

我正在尝试ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"按照主页上的说明设置新计算机并安装Homebrew .问题是,当我运行brew doctor它时给了我这个错误:Your Homebrew is not installed to /usr/local You can install Homebrew anywhere you want, but some brews may only build correctly if you install in /usr/local. Sorry!

当我从终端安装时,我在主目录中.

从我读到的Homebrew应该安装到/usr/local默认情况下所以我不知道我是如何搞砸的...

有关如何将Homebrew安装到正确位置的任何想法?

terminal homebrew

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

:最后一个孩子的作品,:第一个孩子没有

我有aside两个<div class="sku">元素.我正在尝试使用CSS来操纵:first-child它,但它不起作用.但是,当试图访问:last-child它时.

的jsfiddle

HTML

<aside>
  <h1>Product Name</h1>
  <div class="sku">
    <h3>
      100 – Small
    </h3>
    <div class="dimension">
      <ul>
        <li>
          <span class="title">
            Product Dimensions
          </span>
          <span class="specs">
            23.75w
            x
            17.75h
            x
            28d
          </span>
        </li>
      </ul>
    </div>
  </div>
  <div class="sku">
    <h3>
      200 – Large
    </h3>
    <div class="dimension">
      <ul>
        <li>
          <span class="title">
            Product Dimensions
          </span>
          <span class="specs">
            29.75w
            x
            17.75h
            x
            28d
          </span>
        </li>
      </ul>
    </div>
  </div>
</aside>
Run Code Online (Sandbox Code Playgroud)

CSS

.sku:first-child { 
  display:none !important; /*doesn't hide the …
Run Code Online (Sandbox Code Playgroud)

html css html5 css-selectors

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

CKEditor Carrierwave Cloudinary

我想让CKEditor与Carrierwave和Cloudinary一起工作.到目前为止,具有常规文件上载字段的非CKEditor启用视图与Carrierwave和Cloudinary完美配合.但是,当我尝试在CKEditor中上传文件并"将其发送到服务器"时,我得到了一个NoMethodError - undefined method 'each' for "image/jpeg":String:

在从CKEditor类中删除本地存储配置之前,它正在运行,但在本地保存文件.

这是我目前的CKEditor上传器:

class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  include Ckeditor::Backend::CarrierWave

  def extension_white_list
    Ckeditor.attachment_file_types
  end
end
Run Code Online (Sandbox Code Playgroud)

日志文件:

Started POST "/ckeditor/pictures?CKEditor=subsection_content&CKEditorFuncNum=3&langCode=en&authenticity_token=5Bt06UwjUD%2FEdLFANBmZojdv8Hvn2GbQRLvC6h11Dd8%3D" for 127.0.0.1 at 2013-06-20 15:44:18 -0700
Processing by Ckeditor::PicturesController#create as HTML
  Parameters: {"upload"=>#<ActionDispatch::Http::UploadedFile:0x007ff742c77018 @original_filename="pic1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload\"; filename=\"pic1.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/var/folders/0t/l1qc3j596v77_z3s8f2pm75w0000gn/T/RackMultipart20130620-18566-i0av53>>, "CKEditor"=>"subsection_content", "CKEditorFuncNum"=>"3", "langCode"=>"en", "authenticity_token"=>"5Bt06UwjUD/EdLFANBmZojdv8Hvn2GbQRLvC6h11Dd8="}
   (0.4ms)  BEGIN
   (0.4ms)  ROLLBACK
Completed 500 Internal Server Error in 4ms

NoMethodError - undefined method `each' for "image/jpeg":String:
  (gem) cloudinary-1.0.59/lib/cloudinary/carrier_wave/process.rb:100:in `block in transformation'
Run Code Online (Sandbox Code Playgroud)

任何想法赞赏!

更新 - Tal Lev-Ami的工作配置回答 …

ruby-on-rails ckeditor carrierwave cloudinary

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

如何使用Cloudinary输出带有转换的URL作为字符串?

我将通过说这可能正在接近这个错误来作为序言.我要做的是使用data-属性将URL转换为JS .

目前,我正在使用以下方法生成图片代码:

= cl_image_tag(image.asset.filename.to_s, transformation: "scroller", :"data-medium" => image.asset.filename.to_s)
Run Code Online (Sandbox Code Playgroud)

产生这个:

<img src="http://res.cloudinary.com/bucket/image/upload/t_scroller/v1373070863/s1ufy3nygii85ytoeent.jpg" data-medium="s1ufy3nygii85ytoeent.jpg">
Run Code Online (Sandbox Code Playgroud)

我希望能够做到的是输出它(利用t_medium我设置的命名转换):

<img src="http://res.cloudinary.com/bucket/image/upload/t_scroller/v1373070863/s1ufy3nygii85ytoeent.jpg" data-medium="http://res.cloudinary.com/bucket/image/upload/t_medium/v1373070863/s1ufy3nygii85ytoeent.jpg">
Run Code Online (Sandbox Code Playgroud)

目前,cl_image_tag通过使用正确配置的URL生成图像标记,正在进行繁重的工作.这很好,但是我似乎找不到任何关于如何将配置的URL作为没有图像标记的字符串输出的文档(用作data-medium属性).我可以手动配置URL,但我想知道是否有更好的方法?

haml ruby-on-rails cloudinary

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

与Rackspace使用延迟作业时的ActionMailer"Net :: ReadTimeout""emailsrvr.com"

编辑:我正在使用Rails 3.2.17和Ruby 2.0.0p0

我一直有一些ActionMailer关于发送邮件超时的问题; 请参阅ActionMailer Timing out

将默认Unicorn超时增加到2分钟(用于测试)它在大约45秒内完成.我认为处理这个问题的最佳方法是使用Delayed Job,但是,当DJ运行该deliver方法时,它也会超时.下面的堆栈跟踪.

DJ是否适合这项工作?我是否需要增加DJ的超时时间(我以为我读过默认值为6小时)?任何意见将是有益的.

DJ电话:

NotificationsMailer.delay.access_granted(self).deliver
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

[Worker(host:myhost.local pid:37598)] Job Class#access_granted (id=20) FAILED (3 prior attempts) with Net::ReadTimeout: Net::ReadTimeout
   (0.3ms)  BEGIN
   (1.0ms)  UPDATE "delayed_jobs" SET "last_error" = 'Net::ReadTimeout
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:158:in `rescue in rbuf_fill''
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:152:in `rbuf_fill''
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:134:in `readuntil''
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/protocol.rb:144:in `readline''
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:931:in `recv_response''
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:554:in `block in do_start''
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:941:in `critical''
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:554:in `do_start''
/Users/user/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:519:in `start''
/Users/user/.rvm/gems/ruby-2.0.0-p0@mysite/gems/mail-2.5.4/lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!''
/Users/user/.rvm/gems/ruby-2.0.0-p0@mysite/gems/mail-2.5.4/lib/mail/message.rb:2129:in `do_delivery''
/Users/user/.rvm/gems/ruby-2.0.0-p0@mysite/gems/mail-2.5.4/lib/mail/message.rb:232:in `block in deliver''
/Users/user/.rvm/gems/ruby-2.0.0-p0@mysite/gems/actionmailer-3.2.17/lib/action_mailer/base.rb:415:in `block in deliver_mail''
/Users/user/.rvm/gems/ruby-2.0.0-p0@mysite/gems/activesupport-3.2.17/lib/active_support/notifications.rb:123:in `block …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails actionmailer delayed-job

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

无法弄清楚是什么导致我的测试失败

我是rails的新手,我在没有做TDD的情况下构建了一个应用程序,但我现在回过头来试图通过所有的测试.我已经通过了他们中的大多数,但还有一些与我无法弄清楚的同一问题有关.该应用程序也正常运行,我只是无法通过这些测试.

测试失败并提供:

1) ProductsController POST create with valid params assigns a newly created product as @product
 Failure/Error: post :create, {:product => valid_attributes}, valid_session
 Paperclip::AdapterRegistry::NoHandlerError:
   No handler found for "#<File:0x007fc6d17b28f8>"
 # ./app/controllers/products_controller.rb:43:in `new'
 # ./app/controllers/products_controller.rb:43:in `create'
 # ./spec/controllers/products_controller_spec.rb:86:in `block (4 levels) in <top (required)>'

2) ProductsController POST create with valid params creates a new Product
 Failure/Error: post :create, {:product => valid_attributes}, valid_session
 Paperclip::AdapterRegistry::NoHandlerError:
   No handler found for "#<File:0x007fc6d1757cf0>"
 # ./app/controllers/products_controller.rb:43:in `new'
 # ./app/controllers/products_controller.rb:43:in `create'
 # ./spec/controllers/products_controller_spec.rb:81:in `block (5 levels) in <top …
Run Code Online (Sandbox Code Playgroud)

tdd ruby-on-rails paperclip

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

为什么我的Wordpress有随机数的文件名

我是WP的新手,并试图帮助一位朋友清理被黑客入侵的网站.我看到的文件名称如下:

wp-comments-post234.php wp-trackback.php111 571719714.php

有没有办法判断这些文件是否正在使用和/或是否是恶意文件?

wordpress

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

如果存在散列键/值,是否有更有效的方法返回给定数组键的哈希值?

我有一个接受哈希的方法,我有一个键数组(按优先级排序),我想检查哈希值,并返回找到的第一个匹配键的值blank?.到目前为止,我有以下几点,但由于我大量使用这种方法,我想知道是否有更有效的方法来实现它?

result = [:title, :name, :identifier, :slug].each do |key|
            if my_hash[key].present?
              return my_hash[key]
              break
            end
          end 
Run Code Online (Sandbox Code Playgroud)

所以给出以下哈希:

{
  id: 10,
  title: "",
  name: "Foo",
  slug: "foo"
}
Run Code Online (Sandbox Code Playgroud)

我希望result是:"Foo"

ruby ruby-on-rails

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