Reputation: 743
I'm trying to build a form that has an input for a question, and five inputs, each of which represents an answer choice.
My problem is that in my /show.html.erb
file, it throws an error which I can't figure out.
For instance:
QUESTION
ANS1
ANS2
ANS3
ANS4
ANS5
Here's what I have so far:
# app/views/mcqs/new.html.erb
<%= form_for :mcq, url: mcqs_path do |f| %>
<%= f.label :title %><br>
<%= f.text_field :q %>
<%= f.label :ans1 %><br>
<%= f.text_field :ans1 %>
<%= f.label :ans2 %><br>
<%= f.text_field :ans2 %>
<%= f.label :ans3 %><br>
<%= f.text_field :ans3 %>
<%= f.label :ans4 %><br>
<%= f.text_field :ans4 %>
<%= f.label :ans5 %><br>
<%= f.text_field :ans5 %>
<%= f.label :category_id %><br>
<%= f.number_field :category_id %>
<%= f.label :tags %><br>
<%= collection_check_boxes(:mcq, :tag_ids, Tag.all, :id, :name) %>
<%= f.submit %>
<% end %>
The controller:
# app/controllers/mcqs_controller.rb
class McqsController < ApplicationController
def new
@Mcq = Mcq.new
end
def index
@questions = Mcq.all
end
def show
@Mcq = Mcq.find(params[:id])
end
def create
@Mcq = Mcq.new(params[:mcqs])
# @Mcq.save returns a boolean indicating whether the article was saved or not.
if @Mcq.save
redirect_to @Mcq
else
render 'new'
end
end
end
The view:
# app/news/mcqs/show.html.erb
<strong>MCQ Title:</strong><br>
<%= mcq.q %>
<strong>Question Text:</strong><Br>
<%= mcq.ans1 %>
...<strong>MCQ Title:</strong><br>
<%= mcq.q %>
<strong>Question Text:</strong><Br>
<%= mcq.ans1 %>
...
The error:
NameError in Mcqs#show
Showing app/views/mcqs/show.html.erb where line #4 raised:
undefined local variable or method `mcq' for #<#
<Class:0x007fad811936e8>:0x007fad838a47a0>
<strong>MCQ Title:</strong><br>
<%= mcq.q %>
</p><p>
How can I display the input of the new.html.erb
on the show.html.erb
without this error?. <%= @mcq.q %>
doesn't work.
Upvotes: 0
Views: 45
Reputation: 33471
In your show
method you create @Mcq
as Mcq.find(params[:id])
, but then in your show
view you want to access to it as mcq
, so you need to access it the same way (name) as you declared it within the controller.
Try with:
<strong>MCQ Title:</strong><br>
<%= @Mcq.q %>
If you create a @mcq
on the controller (show
method), and then you want to access to it in the view which responds to that method using @Mcq
, then you will receive an object with NilClass
, that's to say, if the names don't match, they won't work.
Also if you use @mcq
on the controller, and then you want to access as mcq
, that won't work neither, the one on your controller is an instance variable, available to be used within your views coming from your controllers, the second one is a local variable, and most probably it'll raise an undefined local variable or method 'variable'
error.
I can quote to @Anhubaw with:
The main difference between local and instance variable is that local variable is only available in controller, where as instance variable is available in corresponding views also. The controller and views do not share local variables
Upvotes: 3
Reputation: 5753
Change all @Mcq
in controller to @mcp
, and change all mcq
in view to @mcq
, also change :mcq
in new view to @mcq
.
The variable prefix with @
means it is an instance variable, it can be accessible in the view, while the normal variable doesn't start with @
is just a local variable, it just can be used in the controller action method.
Upvotes: 1