ruby-on-rails-3.1

Rails 3 initializers that run only on `rails server` and not `rails generate`, etc

三世轮回 提交于 2019-12-18 04:45:38
问题 I have a relatively small piece of initializer code that I want to run whenever rails server runs, but not when I run rails generate , rails console or any other rails command (including rake tasks that require the environment task). This piece of code pre-fills some caches and is relatively expensive so I really don't want it to run on anything but rails s Solutions that are unsatisfactory: Foreman et al. will mean it'll run on a different process which is (a) over the top for that small

Test an HTTPS (SSL) request in RSpec Rails

我是研究僧i 提交于 2019-12-17 23:08:20
问题 I'd like to take advantage of the force_ssl feature in rails 3.1rc4. class ApplicationController < ActionController::Base force_ssl end Problem is this breaks most/all of my existing RSpec controller specs. For example, this fails: describe "GET 'index'" do it "renders the Start page" do get :index response.should render_template 'index' end end Instead of rendering the page the response is a 301 redirect to https://test.host/ . How can I change my specs to simulate an HTTPS GET/POST? And do

Heroku logging not working

痞子三分冷 提交于 2019-12-17 22:42:10
问题 I've got a rails 3.1 app deployed on Heroku Cedar. I'm having a problem with the logging. The default rails logs are working just fine, but when I do something like: logger.info "log this message" In my controller, Heroku doesn't log anything. When I deploy my app I see the heroku message "Injecting rails_log_stdout" so I think calling the logger should work just fine. Puts statements end up in my logs. I've also tried other log levels like logger.error. Nothing works. Has anyone else seen

pdfkit does not style pdfs

痞子三分冷 提交于 2019-12-17 22:39:41
问题 I have a rails 3.1 app that creates pdf documents using pdfkit, and everything works as specified, except for the fact that the generated pdfs don't have any styling. I am assuming that wkhtmltopdf doesn't have access to my stylesheets and that it is not a larger issue than that. Would anyone have a clue as to how you would allow access to these stylesheets? I have basically followed railscast #220 on the subject, however I have had to create a new initializer to get pdfkit to work with rails

heroku run console returns 'Error connecting to process'

跟風遠走 提交于 2019-12-17 22:32:20
问题 I have deployed a rails 3.1 app to Heroku Cedar stack, and am trying to perform a: heroku run rake db:migrate it returns: Running console attached to terminal... Error connecting to process I also try to simply launch the console: heroku run console Any run command returns the same error. Running console attached to terminal... Error connecting to process Looking at the logs I get the error code: 2011-09-25T16:04:52+00:00 app[run.2]: Error R13 (Attach error) -> Failed to attach to process

Where to override current_user helper method of devise gem

对着背影说爱祢 提交于 2019-12-17 21:53:45
问题 How can i override current_user of devise gem. Actually I need to add web services for mobile-app. Currently devise is managing session and 'current_user' for web-application. Now Mobile app will send user_id to the server. I need to override current user like this def current_user if params[:user_id].blank? current_user else User.find(params[:user_id]) end end Should I need to modify devise gem as plugin ? or something else ? Kindly explain in detail as I am new in rails. Kind regards, 回答1:

Rails 3.1+ Nested Forms Issue: Can't mass-assign protected attributes

冷暖自知 提交于 2019-12-17 21:18:00
问题 I have a basketball app, where a Roster has many Players, and a Player can be on multiple Rosters.(Reason for many-to-many is for a Player-Roster archive) Roster.rb class Roster < ActiveRecord::Base belongs_to :team has_many :rosterizes has_many :players, :through => :rosterizes accepts_nested_attributes_for :players attr_accessible :jersey_number, :team_id, :class_year, :players end Rosterizes.rb (poorly named I know...) class Rosterize < ActiveRecord::Base belongs_to :player belongs_to

Chaining methods using Symbol#to_proc shorthand in Ruby?

老子叫甜甜 提交于 2019-12-17 20:05:46
问题 I'm wondering is there a way we can chain a method using (&:method) For example: array.reject { |x| x.strip.empty? } To turn it into: array.reject(&:strip.empty?) I prefer the shorthand notation, due to its readability. 回答1: No, there's no shorthand for that. You could define a method: def really_empty?(x) x.strip.empty? end and use method: array.reject(&method(:really_empty?)) or use a lambda: really_empty = ->(x) { x.strip.empty? } array.reject(&really_empty) but I wouldn't call either of

What is the purpose of config.assets.precompile?

佐手、 提交于 2019-12-17 18:46:51
问题 In Rails 3.1, you must whitelist files that you want included in asset precompilation. You must open up config/environments/production.rb and explicitly include assets you want precompiled: config.assets.precompile += ['somestylesheet.css'] If you don't do this this and you run rake assets:precompile , your asset will not be copied to public/assets, and your app with raise an exception(therefore causing a 500 error in production) when an asset is not found. Why is this necessary? Why aren't

build method on ruby on rails

守給你的承諾、 提交于 2019-12-17 18:27:39
问题 New to rails and I'm following the Depot project found in the Agile web development with rails 3.1. Everything was fine until I got lost when the book used the "build" method. @cart = current_cart product = Product.find(params[:product_id]) @line_item = @cart.line_items.build(product: product) My google searches led me to understand that the .build method is just a cleaner way to create a row in the table (with association between tables). But on the code above, I was expecting the code would