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 %>
manpreet
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:
but I wasn't sure how to do this when I was dealing with a foreach loop:
My futile attempt was to create a helper:
and putting the below inside the loop
However, this didn't work. What would be the best way to pass on a variable outside the if...else statement?