Rails

Scope of rakes in a rails project?

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a number of parsers I run with rakes in a project I'm working on. When using a method name that already exists in another rake, and because they both use the same environment, I get a conflict. Is there a way to limit the scope of rake files within their namespace? I thought that was the whole point of the namespace? Example: namespace :test do task :test_task => :environment do ... end def test_method(argument) ... end end namespace :other_test do task :test_task => :environment do ... end def test_method(argument, argument2) ... end

rails gem prawn, image and anchor

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Can you tell me how to insert image which will be a link to for example page 20? I know how to make with normal text: text "<link anchor='page20'>Go to page 20</link>", :inline_format=>true and then on page 20 I have add_dest('page20', dest_fit(page.dictionary)) but how to do this with image ? 回答1: Prawn does not support this functionality. In fact, even if you placed a formatted_text_box over an image and fill it with white space, it still will not work. An anchor has to include text to work. If you don't mind having text over your image,

Rails - How to declare attr_accessible for multiple roles without duplication

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a way to declare attr_accessible for multiple roles without a ton of duplication? If I have several user roles, and each role is allowed to edit a different subset of attributes, here's what my attr_accessible declaration looks like: attr_accessible :first_name, :last_name, :active, :as => :admin attr_accessible :first_name, :last_name, :as => :manager attr_accessible :first_name, :last_name, :as => :guest I'd like to either A) define an array of accessible attributes that can be shared among different roles or B) define an array of

Undefined method raise_in_transactional_callbacks=&#039; for ActiveRecord::Base:Class (NoMethodError)

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Before writing this question I looked at these answers, but was unable to find a solution.: Error when execute rails generate scaffold User name:string email:string rake aborted! undefined method `migration_error=' for ActiveRecord::Base:Class Error launching Rails server: undefined method 'configure' When I try to start a new application (for Hartl's tutorial , Chapter 2), at the stage scaffold start, I got an error like: **undefined method `configure' for # (NoMethodError)** But thanks to the above examples, I edited the development.rb

Rails greater_than model validation against model attribute

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got a Trip model, which among other attributes has a start_odometer and end_odometer value. In my model, i'd like to validate that the end odometer is larger than the starting odometer. The end odometer can also be blank because the trip may not have finished yet. However, I can't figure out how to compare one attribute to another. In trip.rb: comparing against the symbol: validates_numericality_of :end_odometer, :greater_than => :start_odometer, :allow_blank => true gives me the error: ArgumentError in TripsController#index :greater

Rails 3: validates :presence =&gt; true vs validates_presence_of

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What is the difference between validates :presence and validates_presence_of ? Looking through ActiveModel it looks like they setup the validation the same way. However, given the following model definition: class Account < ActiveRecord::Base has_one :owner_permission, :class_name => 'AccountPermission', :conditions => { :owner => true, :admin => true } has_one :owner, :class_name => 'User', :through => :owner_permission, :source => :user validate :owner, :presence => true validates_associated :owner end Calling save on an instance of

Rails join a list of strings with commas and “and” before the last

匿名 (未验证) 提交于 2019-12-03 02:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Is there a standard rails helper that converts an array of strings like ["apple", "banana", "pear"] into "apple, banana, and pear" for inserting into a sentence? 回答1: Yeah to_sentence ought to work nicely. http://apidock.com/rails/Array/to_sentence 转载请标明出处: Rails join a list of strings with commas and “and” before the last 文章来源: Rails join a list of strings with commas and “and” before the last

Rails: Update model attribute without invoking callbacks

匿名 (未验证) 提交于 2019-12-03 02:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a User model that has a :credits attribute. I want a simple button that will add 5 to the user's credits, through a route called "add" so that /users/3/add would add 5 to the credits of user id = 3. def add @user = User . find ( params [: id ]) @user . credits += 5 redirect_to root_path end That is the relevant part of my controller. The problem is, I dont want to call @user.save because I have a before_save callback that re-encrypts the user's password based on the current UTC time. I just want to simply add 5 to the

Rails named_scope with has_and_belongs_to_many

匿名 (未验证) 提交于 2019-12-03 02:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: i have 3 tables - films, films_genres (for connect 2 tables) and genres. In Model film.rb - has_and_belongs_to_many :genres In Model genre.rb - has_and_belongs_to_many :films So, how I can write this sql code: SELECT * FROM genres INNER JOIN films_genres ON genres .id = films_genres .genre_id WHERE ( films_genres .film_id = 1 ) with named_scope in Model film.rb for show all film rolled genres? 回答1: class Model < ActiveRecord::Base named_scope :by_genre, lambda { |*genres| { :include => :genres, :conditions => [ "genres.id IN (?)", genres.map

RESTful login with devise (Rails 4)

匿名 (未验证) 提交于 2019-12-03 02:52:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How do I do RESTful sign-up and sign-in using devise in Ruby on Rails (I am using version 4)? I could not find any documentation with regards to the parameters (e.g. email, password) that I should POST to the server. It seems that RESTful login using JSON data (e.g. via AJAX) is not supported out of the box in the current version of devise - the default behavior is to send back a whole HTML page for any type of request made, instead of a JSON object for handling JSON request specifically. Does this mean that I need to create/extend custom