I'm trying to build a follow user feature in my app using Railstutorial.org and I'm hitting a error that doesn't make a lot of sence to me.
In my applicaion.html.haml I render 2 partials,
= render '../assets/javascripts/angular-app/templates/stats'
= render '../assets/javascripts/angular-app/templates/follow_form' if user_signed_in?
The first partial (stats) is this,
- #user ||= current_user
.stats
%a{:href => following_user_path(#user)}
%strong#following.stat
= #user.following.count
following
%a{:href => followers_user_path(#user)}
%strong#followers.stat
= #user.followers.count
followers
And resolves without error,
The second partial is this,
- unless current_user?(#user)
#follow_form
- if current_user.following?(#user)
= render 'unfollow'
- else
= render 'follow'
And throws an error, NoMethodError in Application#angular
Showing /home/alucardu/sites/movieseat/app/assets/javascripts/angular-app/templates/_follow_form.html.haml where line #1 raised:
undefined method `current_user?' for #<# <Class:0x007fbe544651f8>:0x007fbe64cdbb10>
I'm using Devise for my registration and user handeling.
On my application.html.haml I have
.welkom
Welkom
= current_user.name
And this hows my first and last name and in the first partial I also use current_user so why am I getting the error undefined methodcurrent_user?'`
There's no current_user? method in devise, if you want to check whether there's a current user or not you can use user_signed_in? method
Related
Another user posts a problem, and I can click on that post to see details about that problem (not through show) and give a recommendation. The thing is that I don't want my recommendation to be linked with this problem. I want it to be linked with that user herself. To do this, I tried:
create
#recommendation = current_user.recommendations.build(recommendation_params)
#user = User.where(user: params[:user_id])
#recommendation.helped_id = #user.id
end
where helped_id should equate that user's id. (later I want that user to be able to see all recommendations she's been given)
But it's turning up error, saying
undefined method `id' for #<User::ActiveRecord_Relation:0x007ff6a9621f68> Did you mean? ids
UPDATE
So I go to the url where this other user's problem is detailed by this view code:
<% #users.each do |u| %>
<%= link_to new_recommendation_path(user_id: u.id) do %>
And the url is: http://localhost:3000/recommendations/new?user_id=2
Could this be the problem?
#user = User.find(params[:user_id]) works fine in new method for showing the problem, but the same code in create method returns cannot find.
You should use the find method which returns the object instead of a relation...
#user = User.find(params[:user_id])
It's possible to continue using AR Relation, so your code would be
#recommendation = current_user.recommendations.build(recommendation_params)
#user = User.where(id: params[:user_id]).first
#recommendation.helped_id = #user.id
Tip: In both scenarios, using User.find or User.where, you should take care of exceptional cases.
Using User.find, if user doesn't exist, then an ActiveRecord::RecordNotFound will be raised.
Using User.where, if user doesn't exist, then a NoMethodError (undefined method `id' for nil:NilClass) will be raised.
The solution was to add nested resources for user and recommendations
resources :users, shallow: true do
resources :recommendations
end
And, in the view, pass both user and recommendation parameters to the recommendation page.
<% #users.each do |u| %>
<%= link_to new_user_recommendation_path(u.id, #recommendation) do %>
Then #user = User.find(params[:user_id]) works.
I searched and searched, but nothing solved my problem. Here's my controller:
def show
#topic = Topic.find(params[:id])
#topic.posts = #topic.posts.page(params[:page]).per(2) # 2 for debugging
end
That functions just fine, because the topic view is reduced to two posts. However, when I add this to show.html.erb:
<%= paginate #topic.posts %>
I'm given this error:
undefined method `current_page' for #<ActiveRecord::Relation:0x69041c9b2d58>
Try with:
def show
#topic = Topic.find(params[:id])
#posts = #topic.posts.page(params[:page]).per(2)
end
And then:
<%= paginate #posts %>
If you get pagination errors in Kaminari like
undefined method `total_pages'
or
undefined method `current_page'
it is likely because the AR scope you've passed into paginate has not had the page method called on it.
Make sure you always call page on the scopes you will be passing in to paginate!
This also holds true if you have an Array that you have decorated using Kaminari.paginate_array
Bad:
<% scope = Article.all # You forgot to call page :( %>
<%= paginate(scope) # Undefined methods... %>
Good:
<% scope = Article.all.page(params[:page]) %>
<%= paginate(scope) %>
Or with a non-AR array of your own...
Bad:
<% data = Kaminari.paginate_array(my_array) # You forgot to call page :( %>
<%= paginate(data) # Undefined methods... %>
Again, this is good:
<% data = Kaminari.paginate_array(my_array).page(params[:page]) %>
<%= paginate(data) %>
Some time ago, I had a little problem with kaminari that I solved by using different variable names for each action.
Let's say in the index action you call something like:
def index
#topic = Topic.all.page(params[:page])
end
The index view works fine with <%= paginate #topic %> however if you want to use the same variable name in any other action, it throu an error like that.
def list
# don't use #topic again. choose any other variable name here
#topic_list = Topic.where(...).page(params[:page])
end
This worked for me.
Please, give a shot.
I have on view show.html.erb
<%= #question.user.lesson_id %>
and this is on browser a number.
my questions_controller.rb is
def show
#question = Question.find(params[:id])
#comments = #question.comments.all
if user_signed_in?
#rating_currentuser = #question.ratings.find_by_user_id(current_user.id)
unless #rating_currentuser
#rating_currentuser = current_user.ratings.new
end
end
and my questions_controller_test.rb
test "should show question signed in" do
sign_in users(:user2)
get :show, :id => #question_user1.to_param
assert_response :success
end
and everything is ok (browser and testing)
when change view show.html.erb
<%= #question.user.lesson.name %>
I am OK with browser but fail testing with
ActionView::Template::Error: undefined method `name' for nil:NilClass.
I try this
#name = Lesson.find(params[:id])
on questions_controller.rb then test fail with
Expected response to be a <:success>, but was <302>
similar questions here here and here
Using try can potentially mask the underlying problem.
For example, when I had this error it was because I wasn't creating a model object without the correct foreign key references fixed up - the view was rendering using the FK association with the name attribute - e.g. Product.ProductType.name. If I had used try to workaround it, it would have masked this fault in my test which would have been undesirable in the test. Once I had added the correct references everything worked as it should have with no need for try(:name).
The solution from this answer
is use the try method and change view as
<%= #question.user.lesson.try(:name) %>
I've been walking through the Hartl tutorial for the first time and have been getting an error on Chapter 10 editing/updating users. Following along, I >>should<< be able to at least view the user edit page, but keep getting the below error. Have gone through everything a few times but can't seem to find the origin of my problem. Note that this is my first time doing anything programming related so go slow with me.
Error
undefined method `model_name' for NilClass:Class
Line being called out in error, from my user edit view
<%= form_for(#user) do |f| %>
Edit method from Users controller
def edit
#user = User.find(params[:id])
#title = "Edit user"
end
A similar block of code is used in my user new view with no issues, and am at a loss why this would be returning nil and have tried with both new and existing users . Thanks!
I just had the same issue, it looks like this happens because your "edit" action isn't defined. Could it be you have either not saved the user_controller file or forgotten to define the "edit" variable?
The error is a bit obscure. It is saying that #user in <%= form_for(#user) do |f| %> is nil. It is likely that #user = User.find(params[:id]) is not finding anything. It would be worth putting Rails.logger.debug("Id: #{params[:id]}") above #user = User.find(params[:id]) and see what is being passed for params[:id].
This SO post had a similar issue though not with the tutorial. The exception is coming from form_for.
My app seems to randomly be throwing a "undefined method `map' for nil:NilClass" error when users are trying to update their profile.
But what's weird is it's saying the error happens on update, but the error line is actually in a view.
Full error:
users#update (ActionView::TemplateError) "undefined method `map' for nil:NilClass"
On line #52 of app/views/users/edit.html.erb
Line 52: <%= options_from_collection_for_select(#networks_domestic, 'id', 'name', #user.network_id) %>
And here are the params from a recent error:
{"user"=>{"email_notify"=>"email#example.com", "network_id"=>"",
"password_confirmation"=>"[FILTERED]", "mobile"=>"", "password"=>"[FILTERED]",
"email"=>"email#example.com"}, "action"=>"update", "_method"=>"put", "id"=>"5089",
"controller"=>"users"}
Honestly not sure where to even start looking. I've had a user say he can update the same info from IE but not from Firefox. And when I use their same info I'm able to update without issue. So, I'm stumped.
Best guess...
Your edit function properly defines #networks_domestic so everything is great until you encounter an error in the update function and call render :action => "edit".
Render does not call the edit function but rather just renders the edit view. So, in the case of a failed update you will have to define #networks_domestic before returning from update.
So say, for example, you have the following:
def edit
#user = User.find(params[:id])
#networkd_domestic = [...]
end
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(params[:user])
flash[:notice] = "User was successfully updated."
format.html { redirect_to(admin_users_url) }
else
format.html { render :action => "edit" }
end
end
end
You will get the error you are describing because #networkd_domestic is not defined in the error condition in the update function.
Add #networkd_domestic = [...] before the edit render and you should be good.
Is #networks_domentic getting set properly in the controller? Add <%= #networks_domestic.inspect %> right before line 52 and see what you get. Check for #networkd_domestic.nil? in the controller and make sure you don't send nil to the view.
EDIT:
If you look at the source for options_from_collection_for_select you will see that it is calling map on the collection you pass (#networks_domestic in this case).