Reputation: 59
I can do
<p>Welcome <%= @current_user.login %></p>
<% @comments.each do |comments| %>
<h3><%= comments.body %></h3>
<% end %>
but not
@cuser = @current_user
@user = User.find(params[:user_id])
@user.comments.create(:user_id => @user.id, :commenter => @cuser.login, :body => params[:comment][:body])
respond_to do |format|
format.js
format.html { redirect_to(@user, :notice => 'Comment was successfully created.') }
Which gives the error "undefined method `login' for nil:NilClass"
If I can use @current_user in one instance why not the other
Upvotes: 2
Views: 3625
Reputation: 71
Use current_user
instead of @current_user
in both the view and the controller.
You probably have a current_user
method on your ApplicationController. That method is creating the @current_user
variable you use in the view.
It is a common practice to use current_user
in both the view and the controller. To check the login, just do current_user.login
. That will work.
Upvotes: 5
Reputation: 15146
Replace @current_user
with current_user
.
It seems, you call current_user
method in controller. current_user
create an instance variable for controller @current_user
. That's why you can see this variable in the view.
You'll catch an error in the views if you'll remove all current_user
method callings in the controller action
Upvotes: 1