Tag: ruby on rails 4

更改密码后用户会话无效,但只有多个线程

我的Rails 4 + Devise 3.2应用程序中的一个function遇到了一个奇怪的问题,它允许用户通过AJAX POST将其密码更改为以下操作,该操作源自Devise wiki 允许用户编辑其密码 。 似乎在用户更改密码之后以及之后的一个或多个请求之后,它们将被强制注销,并在重新登录后继续被强制注销。 # POST /update_my_password def update_my_password @user = User.find(current_user.id) authorize! :update, @user ## CanCan check here as well if @user.valid_password?(params[:old_password]) @user.password = params[:new_password] @user.password_confirmation = params[:new_password_conf] if @user.save sign_in @user, :bypass => true head :no_content return end else render :json => { “error_code” => “Incorrect password” […]

Rails 4,新App:为什么测试在开发环境中运行?

我有一个简单的,新的Rails 4应用程序,当我运行rake test:units ,它会破坏开发数据库,​​即使我在test_helper.rb中设置了RAILS_ENV。 我不会想到的。 以下是重现它的简单步骤。 我有Ruby 2.0.0p247和Rails 4.0.1。 rails new foo rails generate scaffold gadget rake db:migrate 我编辑test / models / gadget_test.rb看起来像这样: require ‘test_helper’ class GadgetTest < ActiveSupport::TestCase test "the env" do assert_equal "test", Rails.env end end 我已经编辑了第一行test / test_helper.rb ENV[“RAILS_ENV”] ||= “test” 成为 ENV[“RAILS_ENV”] = “test” 即便如此,当测试调用rake test:units失败: 1) Failure: GadgetTest#test_the_env test/models/gadget_test.rb:5]: Expected: […]

默认值:Rails资源路由的排除选项

一个小问题: 我正在使用Rails作为我的REST API,但由于它是一个RESTful API我不需要:new或:edit我的任何资源的路由,因为人们只会通过自动JSON请求完全与这个API交互,而不是图形化。 例如,不需要专用的编辑页面。 目前,我需要为每个定义的资源执行类似的操作: # routes.rb resources :people, except: [:new, :edit] :except /config/routes.rb每个资源上的选项:except它没有什么大不了的,但有没有办法定义默认值,所以我不必在每个资源上都指定这个? 我想稍微干掉这段代码,不要做任何蹩脚的事情,比如在任何地方传递默认选项的局部变量。 更一般地说,您可以设置Rails路由的默认选项,除了:exclude ? 谢谢!

使用MailCatcher和Devisegem的Rails 4

我有一个与gemDevise的应用程序。 我需要通过电子邮件确认注册。 在config / environment.rb中我添加了这个 ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { :address => “smtp.gmail.com”, :port => 587, :domain => “gmail.com”, :authentication => ‘plain’, :user_name => “myappname@gmail.com”, :password => “mypassword” } 在config / environment / development.rb中添加: config.action_mailer.default_url_options = { :host => ‘localhost:3000’ } config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => “127.0.0.1”, :port => 1025 } […]

水印图像用回形针,导轨4

我一直在尝试为我的图像添加水印,按照水印中使用回形针列出的答案: Watermark.rb: module Paperclip class Watermark < Processor # Handles watermarking of images that are uploaded. attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, :watermark_path, :watermark_offset, :overlay, :position def initialize file, options = {}, attachment = nil super geometry = options[:geometry] @file = file @crop = geometry[-1,1] == '#' @target_geometry = Geometry.parse geometry @current_geometry = Geometry.from_file @file […]

在Rails 4中的has_and_belongs_to_many关系中使用uniq

我正在尝试在has_and_belongs_to_many关系上实现一个唯一约束,如下所示: class User has_and_belongs_to_many :foos, uniq: true end 因为我在调用foos时只想要独特的foos ,所以我添加了uniq选项。 自升级到Rails 4以来,我开始收到以下警告: 弃用警告:不推荐使用User.has_and_belongs_to_many:foos声明中的以下选项:: uniq。 请改用示波器块。 例如,以下内容: has_many :spam_comments, conditions: { spam: true }, class_name: ‘Comment’ 应该改写如下: has_many :spam_comments, -> { where spam: true }, class_name: ‘Comment’ 我已经尝试了许多不同的组合,并通读源,但无法弄清楚如何编写唯一约束来删除警告?

Rails 4 ckeditor文件上传

我在我的rails projec中使用了ckeditor,我的图像上传有问题。 我不想要ckeditor所拥有的一切,我为它写了一些简单的config.js: CKEDITOR.editorConfig = (config) -> config.language = ‘pl’ config.toolbar_Pure = [ ‘/’, { name: ‘basicstyles’, items: [ ‘Bold’,’Italic’,’Underline’,’Strike’,’Subscript’,’Superscript’,’-‘,’RemoveFormat’ ] }, { name: ‘paragraph’, items: [ ‘NumberedList’,’BulletedList’,’-‘,’Outdent’,’Indent’,’-‘,’Blockquote’,’-‘,’JustifyLeft’,’JustifyCenter’,’JustifyRight’,’JustifyBlock’,’-‘,’BidiLtr’,’BidiRtl’ ] }, { name: ‘links’, items: [ ‘Link’,’Unlink’ ] }, ‘/’, { name: ‘styles’, items: [ ‘Styles’,’Format’,’Font’,’FontSize’ ] }, { name: ‘colors’, items: [ ‘TextColor’,’BGColor’ ] }, […]

在Rails中启用自定义格式化程序

我为Rails编写了一个自定义格式化程序: module Logging class GeneralFormatter < Logger::Formatter def call(severity, time, program_name, message) … end end end 根据Rails指南和这个答案 ,我需要做的就是设置它 config.log_formatter = Logging::GeneralFormatter.new 不幸的是,这似乎不起作用 – 我的自定义格式没有开始。但是,如果我改为定义它: module Logging class GeneralLogger < ActiveSupport::Logger def initialize(*args) super(*args) @formatter = GeneralFormatter.new end end end 然后我可以执行config.logger = Logging::GeneralLogger.new并根据需要格式化我的日志。 设置log_formatter我做错了log_formatter ? 当我想要的只是自定义格式时,我宁愿不定义我自己的Logger。 编辑 (回复评论,并从更多的挖掘中添加更多细节): 我在application.rb设置config.log_formatter并在开发中进行测试,它似乎在一定程度上 Rails.logger.formatter ,因为调用Rails.logger.formatter给了我自定义类。 但这是我在调用Rails.logger.warn(“test”)看到的行为: test打印到控制台(后跟换行符)。 然后输入格式化程序的call方法。 call方法的返回值永远不会打印到控制台。 我认为会发生什么(以及我想要发生的事情): […]

Capybara :: ElementNotFound,但它就在那里

我收到以下错误: Capybara::ElementNotFound: Unable to find field “username” ./spec/controllers/sessions_controller_spec.rb:10:in `block (3 levels) in ‘ 规格: require ‘spec_helper’ describe SessionsController do before :each do @user = FactoryGirl.create(:user) end context ‘creating a new session’ do it ‘can set the current_user variable to the logged user’ do visit ‘/login’ fill_in ‘username’, with: ‘gabrielhilal’ #I have tried `Username` as well […]

在Rails中更新RESTful路由的操作(PATCH或PUT)

我是Ruby on Rails的新手。为什么Rails中RESTful路由的更新操作映射到两个HTTP动词,即PATCH和PUT? PATCH /articles/:id(.:format) articles#update PUT /articles/:id(.:format) articles#update 更新资源(通用CRUD)时,调用两者中的哪个方法?