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 trialMichael Rossiter
2,715 PointsRails 4
I appreciate all the help. I googled/searched stack overflow for 2 hours and just want it to work now.
So...I think it's something to do with the difference between Rails 3 and 4. I'm loathe to downgrade to Rails 3 if I'll just have to learn to handle this in 4 later.
My question: for whatever reason, when I insert <%= status.user.first_name %>, I'm getting an error: "undefined method `first_name' for nil:NilClass"
I associated through the status model - but no luck. If I leave the erb tag as <%= status.user_id %>, I don't get an error, but it also doesn't show the user_id
Thanks for any help!
---Update---
Works now, feel free to grab if you are using Rails 4: https://github.com/michaelrossiter/treebook/
4 Answers
Naomi Freeman
Treehouse Guest TeacherFirst, have you changed all your attr_accessible s?
Take them out of your model and instead do this in the appropriate controller
def profiles_params
params.require(user).permit(:user_id, :friend_id, :users, :friend, :state, :first_name, :last_name, :user_friendship, :full_name, :album, :albums_thumbnail, :title)
end
(using whatever parameters you're currently working with)
And then earlier in the controller, if you have anything that has, for example in the Statuses controller,
def create
@status = current_user.statuses.new(params[:status])
you need to change it to
def create
@status = current_user.statuses.new(status_params)
then at the bottom, it would be
def status_params
params.require(status).permit(:content)
end
So for every model, if you have attr_accessible, you need to get rid of it in the model and do this above set-up in the controller instead.
That might make your user_id appear. It would be a string of numbers.
I'm not sure if that would fix first_name or if that is a separate issue that you're having. So, if you could please post a link to your project on github, that would be super helpful.
If you haven't been doing the github thing, you should :) Makes it easy to share stuff and for others to touch up your code.
Naomi Freeman
Treehouse Guest TeacherHi again! Got your Github. Sent you a pull request. It should be pretty easy to merge it. Click the green buttons and follow the instructions :) I love Github.
Basically there were 2 kinks:
You over-coded lol Super close though. The strong params (that def status_params stuff) only needs to be in 1 place: the controller. Also, each controller only needs, typically, one of those chunks of code. So status has status_params and user has user_params.
You need to always update your params as you create new fields. So your status_params that was in your status controller was a little behind your code. It only allowed :content and :name through. But you don't have a :name field anymore. You have :first_name, :last_name, :profile_name, :full_name
So those need to be added into that set of brackets and the old :name field comes out.
The point of the strong params is to only allow into your database the things you want. It can work against you though if you forget to update it.
I think it should all be working for you now :) Just wanted to explain what I had shifted around.
If you like, you can change .first_name to .profile_name or probably .full_name.
Keep at 'er! Promise it gets easier as you get over this hump setting up all the user profile stuff.
Michael Rossiter
2,715 PointsThanks - weirdest thing now happening - I can now link <%= status.user.full_name %> in show.html but not index.html (same with status.user.first_name)
-----------------UPDATE---------------------------------- NEVERMIND! I had old statuses without a user attached.
HOORAY! IT WORKS!
dan schmidt
2,576 PointsMy rails is rusty and I don't have a prescribed fix for ya off hand, but I can tell you that none of the properties are being set on your status model.
For example:
<% class Status; attr_accessor :foo; end; status = Status.new %>
<%= status %>
<%= status.foo.nil? %>
Outputs:
#<Status:0x00000000bbb6b0>
true