Reputation: 2300
Extreme Rails newbie here. I've created a simple database consisting of a bunch of questions. Each question has an attribute called challengeNumber (an integer) which identifies which challenge the question belongs to.
My goal is to pass the challengeNumber as a parameter in the URL and return only those questions that match that challenge number.
I tried this code:
<h1>Challenge Test</h1>
<% @challenges.each do |challenge| %>
<% if challenge.challengeNumber == (params[:challengeNumber]) %>
<h3><%= challenge.questionText %></h3>
<% end %>
<% end %>
but it returns nothing when using http://localhost:3000/?challengeNumber=1. However, if I use:
<h1>Challenge Test</h1>
<% @challenges.each do |challenge| %>
<% if challenge.challengeNumber == Integer(params[:challengeNumber]) %>
<h3><%= challenge.questionText %></h3>
<% end %>
<% end %>
I get the desired results (questions with a challengeNumber of 1 returned)
Am I doing this correctly? I want to make sure I am using the right conventions, and I would think a parameter of 1 would be passed as an integer, but it appears to be passed as a string. Is it correct to convert this to an integer as shown in my working code, or am I completely off base here?
Edit to add:
Here's some detail on my actual setup:
Here is the challenges show.html.erb, to give you an idea of the structure:
<p id="notice"><%= notice %></p>
<p>
<b>Challengenumber:</b>
<%= @challenge.challengeNumber %>
</p>
<p>
<b>Questionnumber:</b>
<%= @challenge.questionNumber %>
</p>
<p>
<b>Questiontext:</b>
<%= @challenge.questionText %>
</p>
<p>
<b>Answertext:</b>
<%= @challenge.answerText %>
</p>
<%= link_to 'Edit', edit_challenge_path(@challenge) %> |
<%= link_to 'Back', challenges_path %>
So, this DB could have an entry like this:
challengeNumber: 1, questionNumber: 1, questionText: "Why?", answerText: "Because" challengeNumber: 1, questionNumber: 2, questionText: "Who?", answerText: "Me"
challengeNumber: 2, questionNumber: 1, questionText: "How?", answerText: "Wish I knew"
(I realize the naming conventions are off per Danne's reply, will fix once I solve the logic)
So the goal is to send the challengeNumber as a URL parameter (http://localhost:3000/?challengeNumber=1) and have it return both questions associated with that challenge number
Thanks for any help!
Upvotes: 0
Views: 1353
Reputation: 21180
There are a few things you should improve to make it more "Rails".
First of, it is really unnecessary to load all challenges from the database and then loop through them to see which one is the correct. It will consume more memory then needed. Now i don't know how your code looks in the controller but I assume that you do something like this:
@challenges = Challenge.all
That should be changed to
# Edit: Removed .first so that all matching challenges can be retrieved
@challenges = Challenge.where(:challengeNumber => params[:challengeNumber])
That way, only the correct challenge will be loaded from the database and you don't have to care if it is a string or integer. And you can simplify your view to look like this:
<!-- Edit: Changed to looping though @challenges -->
<h1>Challenge Test</h1>
<% @challenges.each do |challenge| %>
<h3><%= challenge.questionText %></h3>
<% end %>
Second, and this is more of a tip to follow the Rails convention. All database fields should be lowercase and separated by underscore so I would recommend to rename challengeNumber to challenge_number
Upvotes: 1