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 Build a Simple Ruby on Rails Application Customizing Forms Creating Relationships

Hello I get stuck at this point,

status.rb and user.rb don't have the attr_accessible info in them, if I try to put it in it causes an error. I keep getting the no method error when trying to edit a status and put an id in. If I open up my database with a db viewer I can see that there is no info stored in the first_name field that I entered for a user.

6 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

(this is probably the 100th time I'm writing this, but it's a good cause ;) )

This application is written in Rails 3 and trying to follow along the videos using Rails 4 will not work because of the differences between those versions. The biggest difference being: attr_accessible no longer allows you to mass assign attributes.

You have two choices, each with its own benefits.

1) Install rails gem version 3.2.19 (I think it's the latest, most secure version of Rails 3) using gem install rails -v 3.2.19, then generate the treebook app from scratch using rails _3.2.19_ new treebook and follow along using that - this should help you with most problems, although you will still encounter different ones along the way (like differences between bootstrap 2 and 3 etc.). This could help you work with legacy Rails 3 apps in the future.

2) Learn the differences berween Rails 3 and 4 and try to 'translate' the app into Rails 4. The main issue will be Strong Parameters in general and Strong Parameters with Devise, so you have to read up on that. This way you learn the newest Rails. Treehouse has Rails 4 course - the Todo app.

Tanner Brandt
Tanner Brandt
12,044 Points

If anyone else runs into this problem and wants to continue using Rails 4 for this tutorial, i've posted a quick workaround on how to use Devise and add extra fields using Strong Parameters in Rails 4.

  1. Ignore everything in this Treebook tutorial that has to do with attr_accessible. You can ignore all of those steps until you reach the 'Customizing Forms' section of this Treebook tutorial.

  2. Once you reach the 'Customizing Forms' section of this Treebook tutorial, open this tutorial: http://www.jacopretorius.net/2014/03/adding-custom-fields-to-your-devise-user-model-in-rails-4.html

  3. Skip ahead in this new tutorial to "Customizing the RegistrationsController," because we already did the first few steps before reaching the 'Customizing Forms' section in the Treebook tutorial.

  4. Following along in this new short tutorial, go to Sublime Text and open up the controllers folder. Create a new ruby file in controllers called: registrations_controller.rb

  5. Copy and paste the following into the registrations_controller.rb file that you created:

class RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:user).permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:user).permit( :email, :password, :password_confirmation, :current_password)
  end
end

6: Then, go to the config folder in Sublime text and open the routes.rb file. Delete the devise_for :users line and replace it with the following:

 devise_for :users, :controllers => { registrations: 'registrations' }
  1. Then, go to your statuses_controller.rb file and, at the very bottom, find this:
# Never trust parameters from the scary internet, only allow the white list through.
    def status_params
      params.require(:status).permit(:name, :content)
    end 

and change it to this:

# Never trust parameters from the scary internet, only allow the white list through.
    def status_params
      params.require(:status).permit(:user_id, :content)
    end 

You are all set! All of your parameters within your form should now be passed to the database when a new user signs up. You can continue forward in the Treebook tutorial.

Thanks Tanner! Works perfectly, first time.

4.1.4

Thanks Maciej I'm going to switch to Rails 3.

Prabhakar 55
Prabhakar 55
476 Points

I am using Rails 4. This is how I fixed my issue. While posting a new status don't select the random user_id like (1,2,34....etc). First open your console and see what is your user_id. Let us say, your user_id 3, keep that in mind!. Now while posting a new status always make sure you only select the user_id as 3 only. If you select any other user_id it will throw you nill class error for first_name. Because when you call @status.user.first_name, here the user does not exist if you select the user_id other than 3. Only user_id 3 exists. So if you call first_name on any user other than 3 it will throw you an error which clearly says "You're calling first_name on nill class which is User."