ruby-on-rails-3

Rails 3.2.x: how to reload app/classes dir during development?

帅比萌擦擦* 提交于 2019-12-23 09:27:28
问题 I have some Rails code that does not fit neatly into a model or controller box. So as per this answer, I created a app/classes directory. Rails 3 seems to automatically add this to the "load path" in Rails, and my application correctly finds the classes I define in there without needing to use require statements. However the code in app/classes does not get reloaded in development mode; if I make a change, I need to restart the server in order to see that change. What's the proper way to make

Gem loads in irb but not console

我们两清 提交于 2019-12-23 09:15:38
问题 This one is driving me nuts. I can load a gem via irb: steve@server:/var/www/listings$ irb irb(main):001:0> Gem.path => ["/home/steve/.gem/ruby/1.9.1", "/usr/local/ruby/lib/ruby/gems/1.9.1"] irb(main):002:0> require 'nokogiri' => true But I can't load it through the rails console: irb(main):001:0> Gem.path => ["/home/steve/.gem/ruby/1.9.1", "/usr/local/ruby/lib/ruby/gems/1.9.1"] irb(main):002:0> require 'nokogiri' => false The gem (nokogiri) is installed steve@server:/var/www/listings$ gem

Controller Action to Delayed Job

白昼怎懂夜的黑 提交于 2019-12-23 09:07:44
问题 I am uploading a tab-delimited document and processing in the controller. Everything works fine, but can take some time on a large file. I want to move this to a delay_job, which I have working elsewhere in my app, but as this is in the controller, cannot be called in the same way. The form calls on the process_file action, which in turn calls on the salesupload action. How should I turn this into a background job? class SalesController < ApplicationController def salesupload(file) uploaded

Set Multiple Environmental Variables On Invocation of Rake Task

空扰寡人 提交于 2019-12-23 09:03:02
问题 I can invoke a Rake task and set a single environmental variable like this: $ ONE=1 rake temp:both But how do I set two environmental variables? This doesn't work: $ ONE=1 TWO=2 rake temp:both This works, but is confusing to read: $ ONE=1 rake temp:both TWO=2 How can I pass more than one env before the call to rake ? 回答1: Agree with @Ernest; it should work. Here's a sample... Sample rake task to echo vars: task :echo_env do puts "VAR1: #{ENV['VAR1']}" puts "VAR2: #{ENV['VAR2']}" end Execute

Ruby on Rails - passing params into 301 redirect in routes.rb

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:01:11
问题 I want to change my existing 'game' routing inside routes.rb, but because of SEO I need also to setup 301 redirect for old links. My old routing: match 'games/:permalink/:id/(:page)' => 'games#show' New routing: match 'gierki/:permalink/(:page)' => 'games#show' Here is redirection which I tried to to do: match 'games/:permalink/:id/(:page)' => redirect {|params| "/gierki/#{params[:permalink]}" + params[:page].nil? ? "" : "/#{params[:page]}" } Above redirect is not working, here is an error:

rspec testing association

无人久伴 提交于 2019-12-23 08:55:40
问题 I want to test that a staff member is associated with a company in my rspec controller tests. I would like to end up with this in my create action of the staff controller: staff.companies << current_company Where current_company is collected from a session variable. How do I write a test for this? I've got these models class Company < ActiveRecord::Base has_many :employees has_many :staff, :through => :employees end class Employee < ActiveRecord::Base belongs_to :company belongs_to :staff end

How do I delegate to a model's to_builder method from a JBuilder view?

流过昼夜 提交于 2019-12-23 08:55:09
问题 Let's say I have a Person class and a Gang class class Person belongs_to :gang attr_accessible :name, :secret def to_builder Jbuilder.new do |app| person.id id person.name name end end end class Gang has_many :people attr_accessible :name end How do I use this to_builder method from a view? For example #app/views/gang/show.json.jbuilder (@gang set by the controller) json.gang do |json| json.name @gang.name json.gang_members(@gang.people) do |person| #how do I delegate to the person.to_builder

Disable CSV downloads in Active Admin

时光怂恿深爱的人放手 提交于 2019-12-23 08:51:07
问题 I am using the Active Admin gem and I would like to hide or remove the links on the index page of each model allowing users to download data as CSV, XML or JSON. Is there any way to do this? 回答1: There is now an option :download_links on the index method, so you omit the download links if you want. For example: ActiveAdmin.register Post do index :download_links => false do # whatever end end 回答2: You should use it as a option of index, but do not separate it from the column functions. Use it

How do I ignore folders and files when pushing to Heroku with a Rails app?

主宰稳场 提交于 2019-12-23 08:49:28
问题 I have a Rails 3.2.8 application and I don't want to push my spec folder and test log to Heroku. How would I do this? What about with a staging remote? Thanks. 回答1: You could put them in .gitignore , but that will exclude them from your repository as a whole. The better solution is to push them to Heroku but prevent Heroku from deploying them to your dynos. You can configure that with a .slugignore file. 来源: https://stackoverflow.com/questions/12523435/how-do-i-ignore-folders-and-files-when

How do I ignore folders and files when pushing to Heroku with a Rails app?

霸气de小男生 提交于 2019-12-23 08:49:20
问题 I have a Rails 3.2.8 application and I don't want to push my spec folder and test log to Heroku. How would I do this? What about with a staging remote? Thanks. 回答1: You could put them in .gitignore , but that will exclude them from your repository as a whole. The better solution is to push them to Heroku but prevent Heroku from deploying them to your dynos. You can configure that with a .slugignore file. 来源: https://stackoverflow.com/questions/12523435/how-do-i-ignore-folders-and-files-when