How to make custom Helpers in Rails 3?

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm fairly new to Rails, and I was confused as to how I could pass local variables outside the if...else statement. It looks like creating a method in the helper file is the conventional way to do this, but I wasn't sure how to do this.

So I'm trying to get the Author of a Mission. If the author of a mission doesn't exist, I want to use the author of its parent Syllabus (missions belong to syllabus). And then I want to print out the username of that author. I was able to do this when I was dealing with only one mission, like:

//controller
@mission = Mission.first
if !@mission.author.blank?
  @author = @mission.author
else
  @author = @mission.syllabus.author
end

//view
<%= @author.username %> 

but I wasn't sure how to do this when I was dealing with a foreach loop:

//controller
@mission = Mission.all

//view 
<% @mission.each do |mission| %>
..(where do I put the logic of finding author? I can't put it in my controller anymore and it won't pass the @author variable outside the if else statement if I put the logic here in the view)..
<%= @author.username %>
<% end %>

My futile attempt was to create a helper:

def author_getter(mission_id)
  @mission = Mission.find(params[:mission_id])
  if !@mission.author.blank? 
    @author = @mission.author 
    return @author
  else
    @author = @mission.syllabus.author  
    return @author
  end 
end

and putting the below inside the loop

<%= author_getter(mission) %>

However, this didn't work. What would be the best way to pass on a variable outside the if...else statement?

profilepic.png
manpreet 2 years ago

Your helper method is a little confused. Helpers shouldn't be poking around in params, they should just be doing things with the arguments they're called with. You're passing in a mission_id but not using it, you're also calling it with (apparently) a mission object when the parameter name indicates than an ID is being asked for. Also, you don't need to be messing around with instance variables in helpers, just plain old variables will do.

Adjust the interface to ask for a Mission object and then use that object:

def author_of(mission)
  mission.author.present?? mission.author : mission.syllabus.author
end

Or, since mission.author should be nil or there, you can take advantage of the falseness of nil:

def author_of(mission)
  mission.author || mission.syllabus.author
end

Then in your ERB:


<% @missions.each do |mission| %>
    <%= author_of(mission).username %>
<% end %>

Of course, once we've simplified and corrected your helper, you might decide that it is too small to be worth bothering with; if so, then you could ditch the helper and do it all in ERB:

<% @mission.each do |mission| %>
    <%= (mission.author || mission.syllabus.author).username %>
<% end %>

However, I think you have this logic in the wrong place: this should be inside Mission itself so that everything (other models, JSON builders, ...) can take advantage of it. So, a method like this would make sense:

class Mission
  def real_author
    author || syllabus.author
  end
end

then you can say this in your ERB:

<% @missions.each do |mission| %>
    <%= mission.real_author.username %>
<% end %>

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.