小编jvn*_*ill的帖子

Ruby on Rails - Paperclip错误

作为参考,我一直在关注本教程:https://devcenter.heroku.com/articles/paperclip-s3除了我现在在localhost测试中,所以我安装了Figaro gem并将我的S3信息放在application.yml中.

使用Rails v4,Cocaine v0.5.3和Paperclip v4.1.0(不确定是否需要提及任何其他gem版本号).

我有一个名为SubmissionDetails的模型,其new.html.erb中我试图将jpg上传到名为attachment的列.以下是相关的型号代码:

has_attached_file :attachment, styles: {
thumb: '200x200>',
large: '800x800>'
}

validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/
Run Code Online (Sandbox Code Playgroud)

当我尝试上传jpg时,它返回到表单,并显示以下错误消息:

1 error prohibited this submission_detail from being saved:
Attachment translation missing:
en.activerecord.errors.models.submission_detail.attributes.attachment.spoofed_media_type
Run Code Online (Sandbox Code Playgroud)

我理解了一些错误,我的en.yml文件中缺少显示此错误消息的文本,但那个欺骗媒体类型部分呢?

这显示在我的服务器控制台中,不确定这是否相关:

[paperclip] Content Type Spoof: Filename header.jpg (["image/jpeg"]), content type discovered from file command: . See documentation to allow this combination.
(0.0ms)  rollback transaction
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails paperclip

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

使用CSS和jQuery进行页面转换

我真的需要你的帮助.我正在创建一个像这样的页面转换:http: //goo.gl/74K2rQ

但是我正在按照这个动画进行我的OWN设计:http://goo.gl/NVjcZ2

到目前为止,我的实验是什么:https://jsfiddle.net/f7xpe0fo/1/

而且它还没有遵循我的设计.要查看我的实际JSFIDDLE,请在此处查看:https://jsfiddle.net/8y801eqh/11/

我的HTML包含的是我用ID的主页和下一页创建了两个div.第一页为红色,下一页为绿色.默认情况下,我隐藏下一页div.看看我的HTML:

<div id="main-page">
 <div>
     <h1>Page Transition</h1>
       <a href="#"> Click Here</a>
</div>

</div>


<div id="next-page">
 <div>
     <h1>This is the 2nd page</h1>
       <a href="#"> Click Here</a>
</div>

</div>
Run Code Online (Sandbox Code Playgroud)

现在为我的CSS我修改他们的设计以匹配整个页面:

#main-page {
  height: 50vh;
  width: 100%;
  position: fixed;
  left: 0;
  background-color: #e74c3c;
 padding: 40px 0 40px 0;
}

h1{
   font-family: Helvetica;
   color: #fff;
    font-weight: normal;
    text-align: center;

}

#next-page{
      height: 50vh;
  width: 100%;
  position: fixed;
  left: 0;
  background-color: …
Run Code Online (Sandbox Code Playgroud)

html css jquery

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

为何坚持下来?ActiveRecord::Rollback 后返回 true 吗?

例子:

BillingProfile.transaction do
  if @billing_profile.save
    unless SomeService.do_something # returns false and rollback occurs
      raise ActiveRecord::Rollback
    end
  end
end

@billing_profile.persisted? # Still return true, despite rollback
@billing_profile.id # Is set, despite rollback
Run Code Online (Sandbox Code Playgroud)

为什么 @billing_profile 的状态不反映记录已回滚?

这是一个问题,因为记录回滚后无法创建。

activerecord ruby-on-rails simple-form ruby-on-rails-4

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

Gemfile.lock生成BUNDLED WITH

我一直在谷歌上搜索,似乎我无法找到解决这个问题的方法.所以这是一件事,我目前正与我的同事一起工作rails项目,它工作正常,直到她改变了她的笔记本电脑并从头开始重新设置她的环境......

每当她改变项目的内容时,Gemfile.lock总是在文件末尾写入:

BUNDLED WITH 1.10.0.rc

每次她不小心提交这个gemfile.lock,其他开发人员工作真的很麻烦,因为它总是告诉我们需要提交/碰撞的东西,因为在我们的环境中,它会自动删除它

gem bundler gemfile.lock

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

在开发中运行rails控制台时,ActiveResource会添加一个额外的类

我们有2个模型:估价和文件.两者都在微服务中,因此我们使用ActiveResource来访问它们.

class Valuation < ActiveResource::Base
  self.site = "#{config.valuation_service.base_url}/valuations"
  self.include_root_in_json = false
end

class Document < ActiveResource::Base
  self.site = "#{config.valuation_service.base_url}/documents"
  self.include_root_in_json = true
end
Run Code Online (Sandbox Code Playgroud)

在开发中运行Rails控制台.

>> Valuation.new(documents: [{ title: 'Foo' }])
=> <Valuation:0x007f9af85f1708 @attributes={"documents"=>[#<Valuation::Document:0x007f9af85f0970 @attributes={"title"=>"Foo"}, @prefix_options={}, @persisted=false>]}, @prefix_options={}, @persisted=false>
Run Code Online (Sandbox Code Playgroud)

所以文档的类名是Valuation:Document.在生产中运行rails控制台时

>> Valuation.new(documents: [{ title: 'Foo' }])
=> <Valuation:0x007f9af595b478 @attributes={"documents"=>[#<Document:0x007f9af595a500 @attributes={"title"=>"Foo"}, @prefix_options={}, @persisted=false>]}, @prefix_options={}, @persisted=false>
Run Code Online (Sandbox Code Playgroud)

文档的类只是Document和它一样尊重配置include_root_in_json.

更大的问题是在调用.to_json对象时.

# development
>> Valuation.new(documents: [{ title: 'Foo' }]).to_json
=> "{\"documents\":[{\"title\":\"Foo\"}]}"

# production
>> Valuation.new(documents: [{ title: 'Foo' …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails activeresource

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

使用rails和postgresql中的键值进行不区分大小写的搜索

我写的搜索功能正在运行,但我希望它不区分大小写.
我怎么能让它变得麻木不仁,虽然我不想改变条件因为它运行得很好.所有四个参数都是可选的,如果它们中的任何一个基于这种条件而存在.

conditions = {}
conditions['first_name'] = params[:first_name] unless params[:first_name].blank?
conditions['last_name'] = params[:last_name] unless params[:last_name].blank?
conditions['specialities.name'] = params[:speciality] unless params[:speciality].blank?
conditions['locations.zipcode'] = params[:zipcode] unless params[:zipcode].blank?

search = Model.using_scope.where(conditions)
Run Code Online (Sandbox Code Playgroud)

我使用PostgreSQL作为区分大小写的数据库.

ruby postgresql activerecord ruby-on-rails

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

将多级数组转换为单级数组?

我想转换这个数组

[[["b", "c"], ["c", "d"]], [["v", "e"], ["r", "g"]]]
Run Code Online (Sandbox Code Playgroud)

[["b", "c"], ["c", "d"], ["v", "e"], ["r", "g"]]
Run Code Online (Sandbox Code Playgroud)

我该怎么转换呢?

ruby

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

如何重构这种方法?

我正在使用Ruby on Rails和Stripe构建一个小型支付处理模块以获得乐趣,我想知道这个方法(用于检查给定用户是否已经使用Stripe存档的卡)可以重构:

class User < ActiveRecord::Base
...
def has_card?
    customer = Stripe::Customer.retrieve(self.stripe_customer_id)
    if customer.cards.count > 0
      true
    else
      false
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我认为这if句话看起来很傻但无法解释为什么(我白天不是开发人员,我只是涉猎)

refactoring ruby-on-rails stripe-payments

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

根据铁轨上的红宝石标签显示相关文章

我试图在文章的显示页面上显示相关文章.当用户查看任何特定文章时,db中的所有相关文章应该根据标记显示在该页面上(在右侧栏中)(因为每篇文章至少有一个标签).我的应用程序在标签和文章之间有关系(请在下面) .

articles_controller.rb

class ArticlesController < ApplicationController

  before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]
    def is_user_admin
      redirect_to(action: :index) unless current_user.try(:is_admin?) 
      return false 
    end

    def index
      @articles = Article.all(:order => "created_at DESC")
      @article_titles = Article.first(10)

      @tags = Tag.all
    end

    def show
      @article = Article.find(params[:id])
    end

    def new
      @article = Article.new
    end

    def create
      @article = Article.new(params[:article])
      @article.user_id = current_user.id

      if @article.save
        flash[:success] = "article created!"
        redirect_to article_path(@article)

      else
        render 'new' 
      end 
    end

    def destroy
      @article = Article.find(params[:id])
      @article.destroy

      redirect_to action:  'index' …
Run Code Online (Sandbox Code Playgroud)

ruby tags ruby-on-rails

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

如何在视图中检查存在参数

我有一个过滤搜索表单:

<%= form_tag admin_publications_path, method: "get", id: "filter", class: "filter" do %>
  <%= label_tag "Category" %>
  <%= select "publication", "category", options_for_select(options_for_categories, selected: params[:publication][:category]), {prompt: 'All'}, onchange: "$('#filter').submit();" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

只要有params [:publication] [:category]可用,这种方法就可以正常工作.

但是如果params不可用,例如在没有先前搜索的情况下渲染视图时,我会收到错误:

undefined method `[]' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)

有没有办法从视图中检查是否存在参数?

谢谢你的帮助,

安东尼

ruby ruby-on-rails

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