Hello!
I'm trying to create a list of user "ads" on my home page in my app.
But I don't get it to work. I'm a rails noob so it might be something
trivial.
I'm currently running into this error:
Couldn't find User without an ID
My pages controller where to home action is looks like this:
def home
@title = "Startsidan"
@user = User.find(params[:id])
@ads = @user.Ad.paginate(:page => params[:page])
end
Hello!
I'm trying to create a list of user "ads" on my home page in my app.
But I don't get it to work. I'm a rails noob so it might be something
trivial.
I'm currently running into this error:
Couldn't find User without an ID
There should be a lot more information available with the error,
including where it has failed. The clue may be in the error message
though, it is trying to find a user and there is no id.
My pages controller where to home action is looks like this:
def home
@title = "Startsidan"
@user = User.find(params[:id])
Possibly this is the problem line, if so then perhaps params[:id] is
nil (hence the error message). Have a look in the log
(log/development.log) and it will tell you what the params are. Are
you passing the id to the home action? Also you could have a look at
the rails guide on debugging to see how to break into your application
using ruby-debug and examine the data at that point.
Could you not top post please, it is easier to follow the thread if
comments are inserted into the previous post. Thanks
Hello Colin!
Thanks for your reply. You are right about where the error is
occurring, this line is causing the error, @user =
User.find(params[:id]).
I haven't passed the id to the home action, where should i do that and
how? Sorry for the noobish question but your help would be gold!
That depends what you are trying to do. If you explain that we may be
able to help. If you do not know what the params hash is all about
then you need to work through some basic tutorials. See the rails
guides at http://guides.rubyonrails.org/ if you are using Rails 3 or
Ruby on Rails guides if using version 2.3.x. Start
with Getting Started obviously, then work through the others.
home action seems to be a member restful action and it needs an id to find the record but you are not declaring that route the right way
show the routes please
and is not what you want since people can still access the original with out ?id=1 gicing user the same message
so what you do is create a route like this
match ‘products/:id’ => ‘catalog#view’ <============copied from the routes.rb comments
you see here the id is specified that means that it the user access product with no id the message will be
no routes matched /products
and that is what we want, not an error in the controller is just that there is no page in the site you can access without specifing
the id.
conclusion
is not a good idea to have a root route that requieres an id instead
I finally got it to work, it turned out that i didn't need to define
@user in my home action, and it was causing the error. Sorry if my
description was unclear. Thanks for your help!
You might take a look at your routes for future reference.
Specifically, take a look at RESTful routing where rails does most of
the work for you. The home path you setup could easily be the index
path for your base controller.