[Ruby on Rails] 5 idioms I always forget
Posted on 2017-08-24
Ruby on Rails is the great web application framework. I usually use it in the current job. But I always forget some idioms. In this post, I try to write out them.
1. Calling helper methods in Model methods
Idiom
ApplicationController.helpers.(helper name)
Example
class Post < ApplicationRecord
def title_asterisk
ApplicationController.helpers.with_asterisk(title)
end
end
2. Calling routing helper methods in Model methods
Idiom
Rails.application.routes.url_helpers.(routing helper name)
Example
class Post < ApplicationRecord
def new_or_edit_path
if persisted?
Rails.application.routes.url_helpers.new_post_path
else
Rails.application.routes.url_helpers.post_path(self)
end
end
end
3. Updating Model’s attributes without saving it to Database
Idiom
model.assign_attributes(attributes)
Example
class PostController < ApplicationController
def update
= Post.find(params[:id])
post .assign_attributes(post_params)
post.save! if post.valid?
post
redirect_to posts_pathend
end
4. Turning off updating updated_at
when saving Models
Idiom
ActiveRecord::Base.record_timestamps = false
Example
class PostImageUpdater
def execute
ActiveRecord::Base.record_timestamps = false
Post.all.find_each do |post|
.image = get_updated_image(post)
postend
ActiveRecord::Base.record_timestamps = true
end
end
5. Basic Auth
Idiom
authenticate_or_request_with_http_basic(realm = "Application", &login_procedure)
Example
class WithBasicAuthController < ApplicationController
:basic_auth
before_action
def basic_auth
"Application") do |name, password|
authenticate_or_request_with_http_basic(== ENV['BASIC_AUTH_NAME'] && password == ENV['BASIC_AUTH_PASSWORD']
name end
end
end
And More…
Maybe there are more idioms I always googling them. If anyone who have a good idea to remember them, please tell me :)