Reputation:
I want to send a variable from controller to partial file.
My controller name is center_controller.rb
and the partial file name is _view_center.html.erb
.
In controller, I have @notes variable. When I give <%= @notes %>
, Iam not getting the values,nil
is coming. Can you help me on that?
center_controller.rb
def view
@notes = Notes.all render"view_center.html.erb"
end
and In my view_center.html.erb
<%= @notes %>
But Iam not getting @notes values, it is nil
.
Thanks in Advance !!!
Upvotes: 0
Views: 596
Reputation: 81
This is wrong
@notes = Notes.all
This is right
@notes = Note.all
Watch out ! When you call a class you want the class name in Singular
First of all you need to name the view to match the controller method.
View file names, by default, match the controller and action that they are tied to.
#center_controller.rb
def view_center
@notes = Note.all
end
#views/center/view_center.html.erb
<%= @notes %>
When you want to render a partial that uses a instance variable, you should do:
<%= render 'view_center', notes: @notes %>
The [notes: @notes] sends the variable to the partial
In the partial file you need to do this:
<% notes.each do |note| %>
<% end %>
Otherwise, take a look at Rails naming conventions, because usually, controller filenames are plural: https://gist.github.com/iangreenleaf/b206d09c587e8fc6399e
Upvotes: 1