Recent notes
RSS feedYou can add if: :query_method and unless: :query_method
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
How to set request parameters (rails 3.2)
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") )
Example Regex format matching
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
another way
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 params.require(:post).permit(..attributes to permit..) end
Security
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=--c880254708d18ce4a686bcd96a25cf0d2117e1e0" Base64.decode64(token.split("--").first) # "...id-salt..."
Note how the salt and id are both exposed! Instead a different token (reset_passowrd_token) should be used.
Passing an array of keys to exclude.
Use the “*” before passing the array in. For example:
PARAMS_TO_SCRUB = [ :created_at, :updated, :id, :format ] params.except!( *PARAMS_TO_SCRUB )
First example's output is incorrect
Everything except the initially html_safe input should be escaped in the output.
The output of the first example should be:
# => "<p>foo</p><br /><p>bar</p>"
Does not symbolize hashes in nested arrays
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 map_value(thing) case thing when Hash symbolize_recursive(thing) when Array thing.map { |v| map_value(v) } else thing end end end
Or, if you want to get really fancy with Ruby refinements (YMMV), one could do
module SymbolizeHelper extend self def symbolize_recursive(hash) {}.tap do |h| hash.each { |key, value| h[key.to_sym] = transform(value) } end end private def transform(thing) case thing when Hash; symbolize_recursive(thing) when Array; thing.map { |v| transform(v) } else; thing end end refine Hash do def deep_symbolize_keys SymbolizeHelper.symbolize_recursive(self) end end end
And later say
using SymbolizeHelper # augmented Hash#deep_symbolize_keys is now available
Does not work with ActionController::BadRequest
This method will not work with ActionController::BadRequest
Compatible with old docs
Library is moved, so old library documentation is compatible http://apidock.com/rails/v3.0.9/ActionController/Streaming/send_file
You could pass a range as the choices parameter
select :object, :method, (1..5)
Requires inheritance of ActionController::Metal
The example code will not work unless class inherits from ActionController::Metal (to get functioning controller) or at least AbstractController:Base.
link_to_if when URL helpers will throw an exception when condition fails
You can use an array to build a URL instead of a URL helper (e.g. edit_admin_user_path(event.user), which would fail when an event has no user:
link_to_if event.user, event.user_name, [:edit, :admin, event.user]
Sometimes, you need the "Oxford comma"
Re: Gramatical error
We invited the strippers, JFK, and Stalin.
versus the appositive phrase:
We invited the strippers, JFK and Stalin.
(Really, you need to see the comic to appreciate the difference.)
Grammatical error
Hi - not sure where I would submit this so just putting here. My apologies if not in the right place.
default: “, and ” - this is grammatically wrong. There should be no comma with the last and.
Example:
[‘one’, ‘two’, ‘three’].to_sentence
should give: “one, two and three”
There is no ‘ .… , and ’ which is considered grammatically incorrect I feel. The ‘and’ does it’s job in the English language quite well by joining the two words it’s in between.
Thank you.
Update for Rails 4
In the example
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { include_blank: true })
It can be updated to
select("post", "person_id", Person.pluck(:name, :id), { include_blank: true })
Right Partitioning Filename extension
1.9.3p392 :013 > x = “picture.2.jpg”
=> "picture.2.jpg"
1.9.3p392 :015 > x.rpartition(‘.’)
=> ["picture.2", ".", "jpg"]
gives a parameter
As a note, you can use it like this:
after_save {|instance|
}
it will pass in the instance being saved.
form_for with :path route
Similar to danwich’s note, if you specify a route using the :path option
resource :posts, path: 'articles'
then the form_for tag must specify the :url option
form_for(@post), url: post_path(@post)
missing :through option
So the way to do the equivalent of a has_many :through is to use has_one :through, with the expected names.
so using the other example we could do
eg
class Person < ActiveRecord::Base belongs_to :team ... end class Task < ActiveRecord::Base belongs_to :person has_one :team, :through => :person end
Article
Here is artice about this method http://blog.envylabs.com/post/75521798481/token-based-authentication-in-rails
Rendering JSONP
If you provide the :callback option with a nil value, then the default JSON object will be returned. As such, this makes creating JSONP response from the render syntax very easy in your controllers, like so:
render json: @object, callback: params[:jsoncallback]
Undocumented pile of ruby
> If you’d like to read someone’s RSS feed with your Ruby code, you’ve come to the right place
No, you’ve definitely come to wrong place. RSS is one of the worst documented libraries I’ve ever seen for Ruby. It’s as confusing and misleading as it can get.
Alternative to :symbol
You can also pass string as an alternative to :symbol
k = Klass.new
k.send “hello”, “gentle”, “readers” #=> “Hello gentle readers”
Symbols more performant than strings
>> options_from_collection_for_select(@posts, :slug, :title, params[:slug])
Consider using symbols for performance, otherwise it will generate a string each time instead of a symbol which will reference the same object.
re: question?
Nope. Read it again:
> This generates a sequence of self.size n-element arrays
If any of the arguments are longer than the receiver, the elements beyond the receiver’s length are ignored
Storing array values
I tried to add a array value using hidden_field_tag and access it in jquery. It just returns the flattened version of array. eg:(If i try to store [1,[1,2,3]] in hidden_field_tag , in jquery iam just getting ‘1 1 2 3’) but if i use input field with type=hidden iam getting the correct value. Why is that?