Tag: ruby on rails

Paperclip在保存后重命名文件

如何在上传和保存文件后重命名文件? 我的问题是我需要自动解析有关文件的信息,以便提出文件应该像我的应用程序一样保存文件名,但我无法访问生成文件名所需的信息,直到记录为止。模型已保存。

当我运行`bundle exec rake test:prepare`它出错了,但是`bundle exec rake db:test:prepare`会很好地发出警告。 这是怎么回事?

所以根据这个链接,一个是快捷包装器(所以我猜它们是相同的)。 当我运行bundle exec rake db:test:prepare ,我收到此错误: Don’t know how to build task ‘test:prepare’ /Users/aj/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval’ /Users/aj/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `’ …但是当我运行bundle exec rake db:test:prepare ,我收到此警告: WARNING: db:test:prepare is deprecated. The Rails test helper now maintains your test schema automatically, see the release notes for details. 任何人都可以阐明这一点吗?

Ruby on Rails:如何在后台运行东西?

创建新资源并且在资源准备好之前需要进行一些冗长的处理时 ,如何将该处理发送到后台 ,它不会阻止当前请求或其他流量到我的网络应用程序? 在我的模型中: class User < ActiveRecord::Base after_save :background_check protected def background_check # check through a list of 10000000000001 mil different # databases that takes approx one hour 🙂 if( check_for_record_in_www( self.username ) ) # code that is run after the 1 hour process is finished. user.update_attribute( :has_record ) end end end

关于将实例变量传递给redirect_to方法的困惑。 正如Rails指南中所见

我正在研究rails指南上的ruby,即http://guides.rubyonrails.org/layouts_and_rendering.html上的“布局和渲染”主题 我很困惑将实例变量传递给redirect_to方法。 这怎么可能? 我认为redirect_to与重定向到另一个网页或url有关。 在指南中给出的示例中,它说明如下: 2.2.2渲染动作视图 如果要在同一模板中呈现与不同操作对应的视图,可以使用带有视图名称的render: def update @book = Book.find(params[:id]) if @book.update_attributes(params[:book]) redirect_to(@book) else render “edit” end end 渲染“编辑”完全有意义,它将再次渲染新forms。 但是redirect_to(@book)在世界上发生了什么? 究竟是什么呈现以及如何将书籍对象重定向到? 顺便说一句,书籍模型有专栏,名称,作者,页面等…

Ruby on Rails Rake资产:预编译错误

我是Rails开发的新手,这是我第一次部署到Heroku。 当我执行我的应用程序(部署后)时出现错误: 2011-09-18T21:05:54+00:00 app[web.1]: Completed 500 Internal Server Error in 10ms 2011-09-18T21:05:54+00:00 app[web.1]: 2011-09-18T21:05:54+00:00 app[web.1]: ActionView::Template::Error (application.css isn’t precompiled): 我搜索了这个,发现我必须预编译我的资产,但是当我尝试这样做时,我发现了另一个错误: $ bundle exec rake assets:precompile RAILS_ENV=production rake aborted! No such process – /usr/lib/ruby/gems/1.8/gems/pg-0.11.0/lib/pg_ext.so (See full trace by running task with –trace) 我还没有找到解决这个问题的方法。 是否有人知道如何解决这个问题?

使用可确认设计 – 当用户尝试使用未经证实的电子邮件登录时,将用户重定向到自定义页面

启用可确认模块后,Devise将不允许未经确认的用户在预定义的时间段过后登录。 而是将用户重定向回登录页面,并显示“您必须先确认帐户才能继续”。 这是一种不受欢迎的交互模型,因为闪存通知没有提供足够的空间来向用户正确解释访问被拒绝的原因,“确认您的帐户”的含义,提供重新发送确认的链接以及如何检查的说明您的垃圾邮件文件夹等。 有没有办法可以更改此行为以重定向到特定的URL?

为什么很多人在Rails中使用“ – %>”而不是“%>”?

对不起这个问题,我认为它更具有offtopic,但我在谷歌上找不到任何东西! 我现在多次看到很多人使用-%>而不仅仅是%> 。 什么意思? 例: image.alt) %> 来源: Rails每6个项目的每个循环插入标记? 在这里,他还使用-%>表示所有块。

Ruby:从没有Rails的gem访问rake任务

我知道可以在Ruby gem中的许多地方定义Rake任务: 在Rakefile里面 内部tasks/*.rake 在lib/tasks/*.rake 我已经读过,当要在gem本身执行任务时,应该使用前两个。 当希望公开任务时,似乎应该选择第三种选择。 网上有许多教程演示了使用Rails从gem加载Rake任务的各种方法,即利用Rails::RailTie 。 但是,我想找到一种在不需要Rails的情况下在另一个gem中使用依赖gem的任务的方法。 有一个简单的解决方案吗? 有人会善意地描述正确的方法,或概述哪些方法可行? UPDATE 我已经尝试创建一个文件bin/my-gem ,以便在系统上用于从my-gem执行Rake任务。 我把以下内容放在里面; #!/usr/bin/env ruby require ‘rubygems’ require ‘rake’ task=ARGV[0] spec = Gem::Specification.find_by_name(‘dsi_core’) Dir[“#{spec.gem_dir}/lib/tasks/*.rake”].each {|file| puts file and Rake::load_rakefile(file)} Rake::Task.clear # Avoid tasks being loaded several times in dev mode Rake::Task[task].reenable # Support re-execution of a task. Rake::Task[task].invoke 部分内容基于此SOpost 。 遗憾的是我做错了,因为在安装gem之后运行my-gem mytask并在lib/test.rake定义了lib/test.rake然后输出以下内容: […]

Rails迁移:尝试将列的类型从字符串更改为整数

我在rails应用程序中使用rails generate migrations命令创建了一个表。 这是迁移文件: class CreateListings < ActiveRecord::Migration def change create_table :listings do |t| t.string :name t.string :telephone t.string :latitude t.string :longitude t.timestamps end end end 然后我想将纬度和经度存储为整数,所以我试图运行: rails generate migration changeColumnType 并且该文件的内容是: class ChangeColumnType < ActiveRecord::Migration def up #change latitude columntype from string to integertype change_column :listings, :latitude, :integer change_column :listings, :longitude, :integer #change longitude […]

Heroku和Rails:Postgres的Gem加载错误,但它在GEMFILE中指定

所以我试图将一个小项目部署到Heroku,但是当我部署它并试图运行heroku run rake db:migrate它返回了我以下错误: rake aborted! Gem::LoadError: Specified ‘postgresql’ for database adapter, but the gem is not loaded. Add `gem ‘pg’` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec’ /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec’ /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:50:in `establish_connection’ /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:120:in `block (2 levels) in ‘ /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval’ /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook’ /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:28:in `block […]