Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Eddie Flores
Eddie Flores
9,110 Points

User relationship with statuses

Okay, I have more than one issue, but let me start out with the more important one.

In the tutorial of "Creating relationships" I've gotten to the point that I have the ability to scroll up or down in the user list -1, 0, 1, 2, 3... and it will not stay to any assigned user. I don't know why. I've pretty much followed the tutorials step-by-step and adjusted for the difference in ruby, rails, bootstrap, and simple_form versions, to the current formats. But now, I am stuck at the point where I put in

<%= @status.user.first_name %> to the user.rb <%= status.user.first_name %> to the index.html.erb

I get NoMethodError in Statuses#index

<% @statuses.each do |status| %> <div class="status"> <strong><%= status.user.first_name %></strong> <p><%= status.content %></p> <div class="meta"> <%= link_to link_to time_ago_in_words(status.created_at) + " ago", status %>

9 Answers

Stone Preston
Stone Preston
42,016 Points

Go through the steps I went through here and make sure you have done all these things. You may have forgotten to add something like the user = current_user .

Eddie Flores
Eddie Flores
9,110 Points

Tried those, and still getting the same issues.

Also, where would you put user = current_user? In status.rb?

Eddie Flores
Eddie Flores
9,110 Points

Also, something else I've noticed is that even when I take all of the edits back, and see that none of my IDs stick to my statuses. I identify user_id 1 or 2 or whichever.. and none of them stick to that status when I come back and check the status or edit it again.

Reb Con
Reb Con
4,607 Points

Just noticed you wrote "link_to" twice in your code snippets above: <%= link_to link_to time_ago_in_words(status.created_at) + " ago", status %>. Is this also in your code? Then this could explain a NoMethodError. If not what else does the error message say?

Eddie Flores
Eddie Flores
9,110 Points

I saw that soon after I posted this topic, and then I removed it, and still have the same issue. The other weird thing is that my name is not showing up at all. I went to rails console, and then did a user = User.first and noticed that my user_id = 3 (which is fine), but that the first_name, last_name, and profile_name variables were all still nil, despite me registering with everything. It's like as if it's not taking anything from those input boxes in the form.

Reb Con
Reb Con
4,607 Points

I had that same problem with these 3 returning Nil at first. Are you using Rails 4?

Eddie Flores
Eddie Flores
9,110 Points

Yup. I've been wondering if that was the issue as well. But, in the beginning I thought I saw them (on the tutorial) using the same version...

Stone Preston
Stone Preston
42,016 Points

Rails 4 could be causing the problem. Have you checked your database to see if you actually have anything in there? Have you seen a "cant mass asign protected attributes error" at all? With Rails 4, you dont add variables to attr_accessible, you have to use a new method of adding "strong parameters" to the controller. The devise github page has more info on how to get it to work with rails 4. I suggest uninstalling rails 4 and just using the version treehouse uses in the videos, which I think is 3.2.

I would check your development database and see if anything is being added to it, when you said your name isnt showing up in the view it makes me think nothing is there.

Reb Con
Reb Con
4,607 Points

I checked out the devise github page link yesterday, but the examples there somehow didn´t work for me (I must have done something wrong, meh). Maybe you have more lucky with this Eddie! I found then this page and followed the steps and it actually made it work (but don´t forget to read the comments below and if you copy&paste the code from the steps: that < sign is supposed to be a < ). So first_name, last_name, profile_name are being saved now and I could go on with the tutorial, yay! But I am not entirely sure what I did there so maybe it´s not recommendable :P. Either way, if I am going to run into further problems with rails 4 and this tutorial I will think about downgrading as well. :/

Stone Preston
Stone Preston
42,016 Points

Nice find Rebecca. Yeah adding the strong parameters will probably fix the rails 4 devise issues. Eddie im pretty sure your database information just isnt getting saved because of the strong parameters. But to be honest, I would still just go through the tutorial again using rails 3.2 since that is what the videos are using so you dont have to deal with these kinds of problems again. Its frustrating I imagine.

Eddie Flores
Eddie Flores
9,110 Points

Thanks Rebecca, this will most definitely help. I will continue forwards with Rails 4, until I just can't figure it out at all. I will concurrently set up another box and run 3.2 from there. If you haven't tried it, there is a free service out there that you can get a ruby/rails box for free. It's pretty awesome. I have several accounts as a way to host several (at the moment, it's only 1 per account), but it's a good way to get a free account. I even run my Javascript box from my iPad (Chrome app). I would suggest it for anyone that is trying to learn something. It's setting up a virtual box, without the hassle and without it taking up resources on your computer. https://www.nitrous.io/join/ySUJiJioAYw

Thanks again!

Reb Con
Reb Con
4,607 Points

Thanks Eddie, interesting. I will check that out.

I'm still having trouble making it work - is there any way to paste the new files you created according to those steps so I can make sure I'm doing it right within the context of our application?

Thanks

Eddie Flores
Eddie Flores
9,110 Points

In that guide that Rebecca posted a link to, there is a piece of the code snippets that have the code "&lt" That means that it's the less than symbol. <

So wherever you see &lt, substitute it with <

That should allow you to start your rails server. I was having that same issue in the beginning until I looked at the code more closely.

Thanks Eddie. I made the change, and I'm still having trouble starting the rails server. It's giving me an error and then exiting; It says that I have syntax errors in initializers/santizers.rb. Here's all of my code for that file, which I pretty much just copy/pasted from the link above:

<code> require "#{Rails.application.root}/lib/user_sanitizer.rb" </code>

Here's the text from the command line error:

C:\Sites\treebook>rails s => Booting WEBrick => Rails 4.0.0.rc2 application starting in development on http://0.0.0.0:3000 => Run rails server -h for more startup options => Ctrl-C to shutdown server Exiting C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.0.rc2/lib /active_support/dependencies.rb:222:in `load': C:/Sites/treebook/config/initiali zers/sanitizers.rb:1: syntax error, unexpected '<' (SyntaxError) <code> ^ C:/Sites/treebook/config/initializers/sanitizers.rb:2: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '(' require "#{Rails.application.root}/lib/user_sanitizer.rb" ^ C:/Sites/treebook/config/initializers/sanitizers.rb:3: syntax error, unexpected '<', expecting $end </code>

Here is the text from the code that I added per the link. 3 files.

  1. sanitizers.rb

<code>

require "#{Rails.application.root}/lib/user_sanitizer.rb"

</code>

  1. user_sanitizer.rb

<code> class User::ParameterSanitizer < Devise::ParameterSanitizer

private

def account_update

    default_params.permit(:email, :password, :password_confirmation, :remember_me,
              :first_name, :last_name, :profile_name)
end

end

</code>

  1. application_controller.rb

class ApplicationController < ActionController::Base

protect_from_forgery with: :exception

def devise_parameter_sanitizer

if resource_class == User

  User::ParameterSanitizer.new(User, :user, params)

else

  super

end end

end

Reb Con
Reb Con
4,607 Points

mh, I think it looks correct. I even replaced your codes with mine and I don´t run into that problem. maybe it´s something else.

Reb Con
Reb Con
4,607 Points

edit: I meant to say "replaced my code with yours"

I really can't figure this out - i may just go back to ruby 3.2. Is there a way to do that so that all my work isn't destroyed?