Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

rails -- treebook profile controller error

I'm building treebook and i've just implemented the named route to allow a user to access a profile page of all a user's statuses by typing hostname/profilename and I get the following error:

SyntaxError in ProfilesController#show c:/railsplay/toybook/app/controllers/profiles_controller.rb:9: syntax error, unexpected end-of-input, expecting keyword_end

Rails.root: c:/railsplay/toybook

Application Trace | Framework Trace | Full Trace Request

Parameters:

{"user_id"=>"1"} Toggle session dump Toggle env dump Response

Headers:

None

I tried passing in the :user_id param in the route, I even tried defining a to_param method on the user model to point to :profile_name, then with :id, neither of those worked. I don't know what else to try.What am I missing? My code did not work like Jason's did in the video.

19 Answers

Nick Fuller
Nick Fuller
9,027 Points

Sorry I am on my phone and trying to make the best of it :)

Try putting the word "else" between the render action: :show and render file:

Then add another "end" after formats: [:html]

in the trace you can see that it passed the correct user id to the browser, but it still didn't load. When I defined the to_param method with either :user_id or :id it actually passed in those strings "user_id" and "id" to the controller. WTH??? I am sooo confused. I've been stuck on this for weeks now.

Nick Fuller
Nick Fuller
9,027 Points

Hi Osgaldor!

You're receiving an unexpected end of input error. This usually means you have an extra end or closing bracket } somewhere in your code.

Can you copy and paste the code from your profiles_controller?

Sure! Here it is: '"ruby

class ProfilesController < ApplicationController
  def show
@users = User.find_by_profile_name(params[:user_id])
if @users
    @posts = @users.posts.all 
    render action: :show
    render file: 'public/404', status: 404, formats: [:html]
  end
end

'''

Sorry it is not formatted in a friendly, readable way. my markdown skills are not yet good.

I apologize, my every attempt to clarify it seems only to serve to further confuse the situation. I hope you are sophisticated enough to make sense of it.

Thank you, Nick, i will try that.

Nick Fuller
Nick Fuller
9,027 Points

Just a couple of comments too on the code if I may? These are meant to be helpful, not derogatory, so please don't take offense. We are all here to learn :)

  1. Your user lookup is a little confusing. You're calling the find_by_profile_name method, but you're passing a user_id parameter. It looks like your parameters are an integer so it's best to use the find method on the model.

  2. I know the Treehouse classes teach to use the "find_by_XXX" method, but try not to use those. They rely on method_missing which is an inefficient means and in Rails 4 these methods have deprecated. There are a couple workarounds to this, you can overwrite the find_by_profile_name method on the user model or you can use active record. Something like.. User.where(profile_name: params[:user_profile_name]).limit(1).first

  3. You're looking for one user right? So it is a little confusing calling the instance variable @[User Test](http://teamtreehouse.com/usertest). I would suggest calling it @user.

  4. In your if statement you call @users.posts.all. This may yield some funky results. I think you're trying to grab all the posts for the user, so you can simply call @user.posts

  5. You're also checking to see if the user variable is set and if it is not you're throwing a 404 error. This is the correct logic and a good practice! However, it's so common that rails will do this for us! If we try to find the user record and it does not exist, Rails will automatically render a 404 page for us! This also means we can remove our calls to the render method.

Here is a refactored version of what you're doing

class ProfilesController < ApplicationController
  def show
    @user = User.find(:user_id])
    @posts = @user.posts
  end
end

I hope this all makes sense!

It makes sense. Thank you! One more thing, how did you make that lovely code block?

When I tried your approach I got the error: Couldn't find User with id=user_id

Here is my current code:

'''ruby

class ProfilesController < ApplicationController
  def show
@user = User.find(:user_id)

if @users
    @posts = @user.posts.all
    render action: :show
else
    render file: 'public/404', status: 404, formats: [:html]
      end
   end 
end

'''

Should that find call be to @users.find(params[:user_id])??

Nick Fuller
Nick Fuller
9,027 Points

Oh gosh, I goofed Osgaldor! I'm sorry, you're right! It should be params[:user_id]

Good catch!! And it looks like you figured out the markdown it's just the "backtick" character to the left of the 1 on your keybord

class ProfilesController < ApplicationController
  def show
    @user = User.find(params[:user_id])
    @posts = @user.posts
  end
end

oh. I was using the apostrophe. No wonder. I got it to work by indenting each line at least 4 spaces.

    User.find(params[:user_id])

It doesn't have to be @user?

When I use it that way it tried to find the user with id of profile name instead of the integer value. WTH???

Should I override the to_param method like this:

    def to_param
     :user_id
    end

i have called the user id :user_id in the posts model. Instead of :id

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Osgaldor Storm I'm a bit confused about where you are with this. What errors are you getting now?

hi Jason! Thanks for repllying! Now when I try to get the profile page I get this:

ActiveRecord::RecordNotFound in ProfilesController#show Couldn't find User with id=User

Extracted source (around line #5): 3 4 5 6 7 8

def show @user = User.find(params[:user_id])

if @users
    @posts = @user.posts.all

Rails.root: c:/railsplay/toybook

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Osgaldor Storm Can you push your code up to GitHub so we can take a look? That will make it easier to troubleshoot. There's a lot of possible places this can go wrong so it would be better to take a look at the code itself.