4.0.2 support
Where did this go in 4.0.2?
Community contributions, tips, and corrections to the documentation. (1708 notes)
Where did this go in 4.0.2?
<%= form_for([:namescope_name, @object], :url => polymorphic_path([:namescope_name, @objectable, @object])) do |form| %>
===for the routes. namescope :admin do resources :articles do resources :comments end resources :photos do resources :comments end en...
Use change_column, and make sure to specify the datatype:
class ChangeUsers < ActiveRecord::Migration
def up
change_column :users, :is_vote_reminder, :boolean, :default => true
end
end
I have confirmed that validates_associated doesn't work with has_one associations, like @amasses said.
This however worked for me, so I recommend to use validates on the has_one association directly, like this:
class Book < ActiveRecord::Base
has_one :cover, validates: true
end
Uniform handling of handling URIs
Flexibility to introduce custom URI schemes
Flexibility to have an alternate URI::Parser
For Example
require 'uri'
uri = URI("http://test.com/posts?id=30&limit=5#time=1305298413")
uri.scheme #=> "http"
uri.host #=> "test.com"
uri.path #=> "/posts"
From the description of Time.current (which is a Rails extension of Ruby's native Time class)
"Returns Time.zone.now when Time.zone or config.time_zone are set, otherwise just returns Time.now."
so 3.days.ago, for example, is in the application's configured timezone (if it's configured).
reject_if: proc { |attributes| attributes['name'].blank? } Has saved me after an 3 hours of google search. Thanks
This is a nice way to add flash if you don't have a any logic that needs to go around your flash.
def destroy
@current_user_session.destroy
respond_with @current_user_session do |format|
format.html {redirect_to login_path, notice: "You have been logged out"}
end
end
In order to prevent fields_for from rendering a hidden field to store the ID of the record use include_id: false instead of hidden_field_id: false
request.original_url # => "http://www.example.com/articles?page=2"
You can make the callback conditional:
before_save :before_method, if: :needs_before_method?
private
def needs_before_method? false end
def before_method # .. end
In Rails 3.2, this seems to work to create a TestRequest based on a certain url:
ActionController::TestRequest.new( Rack::MockRequest.env_for("http://something.tld/foo?one=two&three=four") )
for phone and email
validates_format_of :phone, with: /\\A(\\d{10}|\\(?\\d{3}\\)?[-. ]\\d{3}[-.]\\d{4})\\z/
validates_format_of :email, with: /\\A[\\w]([^@\\s,;]+)@(([\\w-]+\\.)+(com|edu|org|net|gov|mil|biz|info))\\z/i
Perhaps a more efficient way:
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "The post #{@post.title} was added to the system."
else
render action: 'new'
end
end
where post_params is:
private
def post_params
par...
In regards to @aamer's comment on including the password salt this is a bad idea. ActiveSupport::MessageVerifier is NOT encrypted so:
verifier = ActiveSupport::MessageVerifier.new('secret')
id = 'id'
salt = 'salt'
verifier.generate("#{id}-#{salt}") # "BAhJIgxpZC1zYWx0BjoGRVQ=-...
Use the "*" before passing the array in. For example:
PARAMS_TO_SCRUB = [ :created_at, :updated, :id, :format ]
params.except!( *PARAMS_TO_SCRUB )
Everything except the initially +html_safe+ input should be escaped in the output.
The output of the first example should be: # => "
foo
<br /><p>bar</p>"If you have a nested structure containing arrays of hashes, you still need to do that on your own, eg.
module SymbolizeHelper
def symbolize_recursive(hash)
{}.tap do |h|
hash.each { |key, value| h[key.to_sym] = map_value(value) }
end
end
def m...
This method will not work with ActionController::BadRequest
Library is moved, so old library documentation is compatible http://apidock.com/rails/v3.0.9/ActionController/Streaming/send_file