ruby-on-rails-3

Rails 3.1 has_one nested resource: routing not generating “all” paths

拥有回忆 提交于 2020-01-31 19:33:05
问题 I have a has_one relation: # supplier.rb has_one :presentation ... # presentation.rb belongs_to :supplier ... and the folowing nested routes for them: # routes.rb resources :suppliers do resource :presentation end running rake routes gives: supplier_presentation POST ... {:action=>"create", :controller=>"presentations"} new_supplier_presentation GET ... {:action=>"new", :controller=>"presentations"} edit_supplier_presentation GET ... {:action=>"edit", :controller=>"presentations"} GET ... {

Rails3 custom logging options?

二次信任 提交于 2020-01-31 18:42:19
问题 What are the "best-practice" custom logging options for Rails3? Can anyone suggest good gems, or techniques, for logging. In general, what is the convention for custom, non-controller, non-model stuff? If I make a custom logger like this: #custom_logger.rb class CustomLogger < Logger def format_message(severity, timestamp, progname, msg) "#{msg}\n" end end logfile = File.open(RAILS_ROOT + '/log/custom.log', 'a') #create log file logfile.sync = true #automatically flushes data to file CUSTOM

Error when trying to start server after upgrading to Rails 3

℡╲_俬逩灬. 提交于 2020-01-30 05:23:40
问题 I've just upgraded to Rails 3, using the instructions from this railscast. When I try to start the server, this is what happens: $ rails s script/rails:6:in `require': no such file to load -- rails/commands (LoadError) from script/rails:6:in `<main>' I can't find this exact error using Google. Anyone know what is causing this? Thanks for reading EDIT: Here's the console output: $ rvm 1.9.2 $ rails -v Rails 3.0.1 $ ruby -v ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0] $ rails

Rails 3, apache & passenger, send_file sends zero byte files

扶醉桌前 提交于 2020-01-30 04:10:12
问题 I'm struggling with send_file with rails 3.0.9 running ruby 1.9, passenger 3.0.8 on apache on ubuntu lucid The xsendfile module is installed and loaded into apache root~# a2enmod xsendfile Module xsendfile already enabled Its symlinked correctly in mods-enabled lrwxrwxrwx 1 root root 32 Aug 8 11:20 xsendfile.load -> ../mods-available/xsendfile.load config.action_dispatch.x_sendfile_header = "X-Sendfile" is set in my production.rb using send_file results in zero byte files being sent to the

Make user-inputted url into external link in rails

☆樱花仙子☆ 提交于 2020-01-29 17:54:29
问题 I want users to be able to enter a url and then put a link to that url in my view. Valid inputs could be e.x. https://www.google.com/path , http://www.google.com , www.google.com Is there are standard rails way to 1) validate that input is a valid url format and 2) convert that third format to http://www.google.com in my views so the external link works? I don't want to re-invent the wheel if I can avoid it. 回答1: Check this out: validates_format_of :website, :with => URI::regexp(%w(http https

Disabling JavaScript when using Capybara + Selenium

回眸只為那壹抹淺笑 提交于 2020-01-29 17:45:26
问题 I have an app that's designed to still be functional when JavaScript is disabled, so I wanted to write some specs that covered those cases. I'm using Selenium (Firefox) with Capybara and I'm registering an new driver with JavaScript disabled (via Selenium's javascript.enabled property) # spec/rails_helper.rb Capybara.configure do |config| config.ignore_hidden_elements = true config.default_driver = :selenium end Capybara.register_driver :disable_js do |app| profile = Selenium::WebDriver:

Scoping date attribute for this week?

こ雲淡風輕ζ 提交于 2020-01-29 10:06:51
问题 I am trying to scope all of my Products for this week, so it should show all the products leading up to whichever day of the week. class Product < ActiveRecord::Base attr_accessible :purchase_date def self.this_weeks where("purchase_date >= ?", Date.at_beginning_of_week - Date.at_end_of_week) end create_table :products do |t| t.date :purchase_date end end This gives me an error though: undefined method `at_beginning_of_week' What do I need to correct? 回答1: at_beginning_of_week has been

ActiveRecord: Update a record if exists else create?

别来无恙 提交于 2020-01-29 03:18:49
问题 Trying to implement an create if not exists else update record in Active Record. Currently using: @student = Student.where(:user_id => current_user.id).first if @student Student.destroy_all(:user_id => current_user.id) end Student = Student.new(:user_id => current_user.id, :department => 1 ) Student.save! What would be the correct way of updating the record if it exists or else create it? 回答1: You may be looking for first_or_create or something similar: http://guides.rubyonrails.org/v3.2.17

IDs getting mixed up in URL

江枫思渺然 提交于 2020-01-26 04:18:05
问题 So I am relatively new to Ruby on Rails and am building a project management system for an independent study at my University using the language. So far I have created: an an area where a user can create a multiple projects(http://i.imgur.com/pXOBg.png) and in each project a user can create multple to-do lists (http://i.imgur.com/FT3no.png) within each to-do list, a user can create multiple tasks which can be marked as complete/incomplete (http://i.imgur.com/DMdcN.png) Everything's related

Send multiple invitation using devise-invitable gem in rails

て烟熏妆下的殇ゞ 提交于 2020-01-25 20:36:05
问题 I want to send multiple invitations at a time using devise-invitable gem. new.html.erb <h2> <%= t "devise.invitations.new.header" %> </h2> <%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %> <%= devise_error_messages! %> <div> <% resource.class.invite_key_fields.each do |field| -%> <p><%= f.label field %><br /> <%= f.email_field field, placeholder: "Invitation email" %></p> <% end -%> <%= f.collection_select(:role_id, Role