I've been writing applications with Ruby on Rails I was curious about it.
This guy that comes with every time you create a controller 〇〇_helper.rb What is this file used for ... Since it says helper, is it related to helper methods ... ??
At this time, I "It must be a convenient file because it is created at the same time." I searched Qiita for commentary articles as I was interested.
It looks like a file that describes your own helper method. (Self-made helper == custom helper) Since the helper method has only used the things provided by Gem until today, I intuitively thought that this would be useful.
I will refer to other people's Qiita articles This time I used it for code refactoring of my personally developed app. The refactoring content in the controller is the content of this article.
Rails 5.2.3 Ruby 2.5.1
First, let's look at the controller that refactors.
users_controller.rb
class UsersController < ApplicationController
~abridgement~
private
def set_user
@user = User.find(params[:id])
end
def check_user
redirect_to new_user_registration_path, alert: 'Please log in or register' unless current_user.id == @user.id
end
end
Because the less than unless in the method called check_user is not cool
Create a helper method and refactor it.
helpers/users_helper.rb
module UsersHelper
def current_user?(user)
current_user.id == user.id
end
end
I created a helper method called current_user ?.
This method name and content seems to be quite standard.
Directly under the directory called helpers
There is also a file called application_helper.rb
It's like a file that manages helper methods that you apply to multiple controllers and views.
users_controller.rb
class UsersController < ApplicationController
include UsersHelper
~abridgement~
private
def set_user
@user = User.find(params[:id])
end
def check_user
redirect_to new_user_registration_path, alert: 'Please log in or register' if !current_user?(@user)
end
end
When using the helper file's own helper in the controller It seems that it is necessary to read with include as in the second line.
Also, the description of unless has been changed to the description of if! ~.
This made the code a little cleaner.
Refactor posts_controller with similar content.
posts_controller.rb
class PostsController < ApplicationController
def edit
redirect_to new_user_registration_path, alert: 'Please log in' unless current_user.id == @a_post.user_id
end
def destroy
redirect_to new_user_registration_path, alert: 'Please log in' unless current_user.id == @a_post.user_id
if @a_post.destroy
redirect_to root_path, notice: 'Deleted post'
else
render :show, notice: 'The post could not be deleted'
end
end
private
def set_post
@a_post = Post.find(params[:id])
end
end
Here too, we will modify the description below unless.
First, create a helper method.
helpers/posts_helper.rb
module PostsHelper
def current_user_post?(a_post)
current_user.id == a_post.user_id
end
end
We will apply this to the controller.
posts_controller.rb
class PostsController < ApplicationController
include PostsHelper
def edit
redirect_to new_user_registration_path, alert: 'Please log in' if !current_user_post?(@a_post)
end
def destroy
redirect_to new_user_registration_path, alert: 'Please log in' if !current_user_post?(@a_post)
if @a_post.destroy
redirect_to root_path, notice: 'Deleted post'
else
render :show, notice: 'The post could not be deleted'
end
end
private
def set_post
@a_post = Post.find(params[:id])
end
end
This is also done after loading the helper and refactoring.
This time I wrote an article focusing on the controller, but it can be used not only for self-made helpers but also for views. Also, when using it for a view, unlike the controller, it seems that there is no need to describe reading. I'll also refactor the view file code after this.
If you find any deficiencies, please let us know in the comments.
--Helper loading and priority in Rails 5.0 -Summary of Rails helper methods (frequently used)
Recommended Posts