Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Course Queries Syllabus Queries 2 years ago
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.
Turn Your Knowledge into Earnings.
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?
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.
params
mission_id
mission
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:
mission.author
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 %>
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.
Course Queries 4 Answers
Course Queries 5 Answers
Course Queries 1 Answers
Course Queries 3 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.