Specify your own template
You can specify you own template this way:
def notice
...
@template = "some_other_name.html.erb"
end
Community contributions, tips, and corrections to the documentation. (1708 notes)
You can specify you own template this way:
def notice
...
@template = "some_other_name.html.erb"
end
Check out this simple example:
"Hello Ruby friend".sub(/^(.)e/, 'X') # => "Xnd" "Hello Ruby friend".sub(/^(.?)e/, 'X') # => "Xllo Ruby friend"
The question mark turns the dotstar into non-eager mode which means it will halt on the first subsequent "e" rather than the last one. This comes in...
If you are using the aasm plugin/gem, this will generate all named scopes for your various states.
==== Code example
Class Article < ActiveRecord::Base
include AASM
aasm_initial_state :created
aasm_state :published
aasm_state :unpublished
aasm_state :deleted
aa...
or any other chained method call type that you'd like to stub, example:
in your controller:
def new @user = current_site.users.new end
in your spec:
it "#new should assign a @user" do u = mock("User") controller.stub_chain(:current_site, :users, :new).and_return(u)...
Since there's no way to edit posts on here, I need to correct myself and say that what I posted before doesn't work, since you can't specify layout multiple times:
class OrdersController < BaseController layout :determine_layout, :only => :new layout "public", :except => :new
end...
Use FileUtils::copy instead. It is also in 1.8.x, FileUtils, so call that one instead.
mkdir will only create a single directory on an existing path. If you want to create a full path, like the mkdir -p /full/path command, use the makedirs method.
1.8: File.makedirs(path) 1.9: FileUtils.makedirs(path)
Worth noting that if you have a controller which inherits from another controller which has a layout, and in this child controller you're determining the layout at runtime using a method for specific actions, the other actions you are excluding will not inherit the layout from the parent controller....
If you specify +:conditions+ in your +default_scope+ in form of a Hash, they will also be applied as default values for newly created objects. Example:
class Article
default_scope :conditions => {:published => true}
end
Article.new.published? # => true
However:
class Article...
configure session store in config/initializers/session_store.rb
Don't call .save or .update_attribute on other objects inside before_save callback.
Saving other objects inside of before_save callback results in flushing changed hash and the original object is not updated.
UPDATE observed sometimes, still investigating
See ActiveRecord::ConnectionAdapters::TableDefinition#column for details of the options you can use.
This method returns a Pathname object which handles paths starting with a / as absolute (starting from the root of the filesystem). Compare:
>> Rails.root
=> #<Pathname:/some/path/to/project>
>> Rails.root + "file"
=> #<Pathname:/some/path/to/project/file>
>> Rails.root + "/file...
Since Rails version 2, this instance method no longer exists.
You may be looking for its namesake class method, ActiveRecord::Base.method_missing
Note that belongs_to does not support :through option like has_many (although IMHO it would make sense in some cases), but you can easily simulate it with delegate.
For example:
class Person < ActiveRecord::Base
belongs_to :team
...
end
class Task < ActiveRecord::Base
belo...
The example regex for RFC 2822 email is too lenient. Sure it's just an example, but it wound up in our code and we just had to fix it to match the RFC.
The regex as given allows any non-@ non-whitespace characters. But the RFC only allows these characters in mailbox names
alpha digit - ! \\...
Note that the ActiveSupport library provides the +except+ and +except!+ methods, which return the Hash minus the given keys. So you don't need to write your own wrapper if you happen to be using Rails or ActiveSupport as a stand-alone library:
The ampersand is more common, but the W3C recommends that all web servers support semicolon separators in the place of ampersand separators:
http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.2.2
If you want to get the aggregated scoping options of a chain of named scopes use ActiveRecord::Base.current_scoped_methods
It works in the fashion of:
Shirt.red.medium.alphabetical.current_scoped_methods
# ==>
{
:create => {},
:find => {
:conditions => {:color => 'red',...
===Delete all files in log require 'FileUtils' FileUtils.rm_rf(Dir.glob("log/*"))