Tag: ruby on rails 4

如何在控制器规范中禁用before_action?

我在我的控制器规范中使用了这个: controller.class.skip_before_action 具体来说,在这种情况下: controller.class.skip_before_action:require_authorisation_to_view_materials MaterialsController: class MaterialsController < ApplicationController before_action :set_material, only: [:show, :edit, :update, :destroy] before_action :require_authorisation_to_view_materials, only: [:index, :show] def require_authorisation_to_view_materials # For Materials Page unless user_signed_in? && current_user.can_view_materials? redirect_to root_path, alert: "You are not authorised to view the Materials page." end end # GET /materials # GET /materials.json def index @materials = […]

Rails国际化:i18n查找嵌套的语言环境目录

我正在尝试使用嵌套文件结构组织我的本地化文件,以便更容易查找。 我跟着 在rails app中组织Locale文件 你如何在Rails中构建i18n yaml文件? 但是我的翻译版本丢失了: en.view.fruits.apple 。 我认为Rails试图只查找locales/en.yml文件中的翻译但不locales/en.yml子目录,尽管我已将它们包括在内。 配置/ application.rb中: config.i18n.load_path += Dir[“#{Rails.root.to_s}/config/locales/**/*.{rb,yml}”] 我的语言环境目录: |locales |-en.yml |-views |–en.yml 区域设置/查看/ en.yml: en: fruits: apple: “apple” 意见/ fruit.html.haml: = I18n.t(‘views.fruits.apple’)

使用cache_digests与XML构建器?

有没有办法在Rails 4(cache_digests)中利用XML的新缓存策略? 我想我可以使用xml.erb视图,但我更喜欢xml.builder视图的简洁性。 有没有办法以这种方式使用cache_digests?

Rails替换从has_many嵌套属性表单添加到它的集合intead

我有这些模型(为便于阅读而简化): class Place < ActiveRecord::Base has_many :business_hours, dependent: :destroy accepts_nested_attributes_for :business_hours end class BusinessHour < ActiveRecord::Base belongs_to :place end 而这个控制器: class Admin::PlacesController < Admin::BaseController def update @place = Place.find(params[:id]) if @place.update_attributes(place_params) # Redirect to OK page else # Show errors end end private def place_params params.require(:place) .permit( business_hours_attributes: [:day_of_week, :opening_time, :closing_time] ) end end 我有一个动态的表单,通过javascript呈现,用户可以添加新的开放时间。 […]

ActiveRecord :: PendingMigrationError

所以,我刚刚向应用程序添加了一些新的迁移,我随后运行了rake db:migrate。 但是,我似乎仍然得到以下错误。 我也运行了rake db:migrate RAILS_ENV = development命令。 当我刷新应用程序时,我得到了同样的错误。 ActiveRecord::PendingMigrationError Migrations are pending; run ‘rake db:migrate RAILS_ENV=development’ to resolve this issue. 让我知道我可以包含哪些其他文件来帮助解决此问题。

Brakeman Error – 未转义的模型属性

我得到了很多错误,如下所示 Unescaped model attribute near line 20: show_errors(Objective.new(objective_params), :name) 扩展视图 这是我的代码 module ApplicationHelper # Error Helper for Form def show_errors(object, field_name) if object.errors.any? && object.errors.messages[field_name][0].present? “” + object.errors.messages[field_name][0] + “” else return “” end end end

rails 4.0中有多个“root to”路由

我试图让rails根据子域名转到不同的控制器#动作,这是我到目前为止在routes.rb中所拥有的。 Petworkslabs::Application.routes.draw do get ‘/’, to: ‘custom#show’, constraints: {subdomain: ‘/.+/’}, as: ‘custom_root’ get ‘/’, to: “welcome#home”, as: ‘default_root’ end rake显示了我想要的正确路线 rake routes Prefix Verb URI Pattern Controller#Action custom_root GET / custom#show {:subdomain=>”/.+/”} default_root GET / welcome#home 但由于某种原因,我无法获得像abc.localhost:3000这样的请求来命中自定义控制器。 它总是路由它欢迎#home。 有任何想法吗? 我对rails很新,所以关于一般调试的任何提示也将受到赞赏。 编辑:我使用调试器逐步完成代码,这是我发现的 (rdb:32)request.domain“abc.localhost”(rdb:32)request.subdomain“”(rdb:32)request.subdomain.present? 假 看起来由于某种原因,rails认为子域不存在,即使它在那里。 我不知道是不是因为我正在做这个本地主机。

使用Paperclip和AWS S3的自定义URL

我们正在使用带有aws-sdk gem的Paperclip在我们的Rails应用程序中存储和显示图像: class User ‘, medium: ‘600×600>’, thumb: ‘200×200>’ }, default_url: ‘missing_photo.png’ end 如果我在html.erb文件中使用 ,我会得到以下HTML: <img src="https://s3.amazonaws.com//users/images/000/000/001/medium/my_image.png?1419989041″> 。 如何将https://s3.amazonaws.com/作为自定义URL(如https://example.com ? 我的域名全部设置在Cloudfront及其SSL证书中。 我查阅了Paperclip S3 Storage文档 。 有一个:url选项,但我为该选项编写的任何内容似乎都无效。

ActiveAdmin表单不保存嵌套对象

使用ActiveAdmin和Rails 4,我有两个模型, Document和Attachment ,它们之间有一对多的关系。 # models/document.rb class Document < ActiveRecord::Base has_many :attachments accepts_nested_attributes_for :attachments end # models/attachment.rb class Attachment < ActiveRecord::Base belongs_to :document end 我注册了模型,并为每个模型中的所有字段包含了permit_params 。 现在我在下面的代码中使用了表单视图中的has_many 。 这显示了添加附件的选项,它工作得很好。 # admin/document.rb ActiveAdmin.register Document do permit_params :title, :description, :date, :category_id show do |doc| attributes_table do row :title row :description row :attachments do doc.attachments.map(&:document_path).join(“”).html_safe end end end […]

条带令牌未携带到控制器导轨4

问题 我已经测试了CoffeeScript,并且表单调用Stripe,使用正确的响应令牌设置隐藏字段并提交表单。 我的问题是,一旦提交控制器似乎没有正确获取令牌并抛出此错误: Stripe :: InvalidRequestError – 您必须提供卡或客户ID 。 接下来,我累了拿出生成的令牌并将其硬编码到控制器中,看看是否可行。 我提交了表格,在Stripes结束时收到了付款。 关于接下来要尝试什么,我几乎没有想法。 我想知道我是否遗忘了某些东西或遗漏了某些东西,因为付款是在作业下嵌套的。 gem版本 Ruby:2.1.0 Rails:4.0.1 条纹:1.9.9 档 /payment/new.html.erb prohibited this subscription from being saved: This assignment has already been paid for. JavaScript is not enabled and is required for this form. First enable it in your web browser settings. payment_controller.rb class PaymentsController “Payment received, […]