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

First name and last name have no value, possibly due to strong parameters

I have just found out that first_name and last_name have no value and I am using version 4.1 of ruby. Here is my code for user.rb:

class User < ActiveRecord::Base

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

has_many :statuses

def full_name first_name + " " + last_name end

def create user.create(user_params) end

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

I have a feeling that it is something to do with strong parameters but I am not sure if I am using it correctly

2 Answers

Chris Dziewa
Chris Dziewa
17,781 Points

In your create method you need to change user to User. I think your strong parameters are fine. Let me know if that fixes it.

Unfortunately it is still not working the error that is being outputted is: undefined method +' for nil:NilClass app/models/user.rb:13:infull_name'

Is there anything else that could be causing this error? maybe a migration file etc. Or do I have to just scrap this tutorial since it is using a older version?

Chris Dziewa
Chris Dziewa
17,781 Points

I know that in another tutorial, all of the strong parameters information was placed in the user controller and not the model. Maybe you can use some info from that controller. Here it is:

class UsersController < ApplicationController
  def show 
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create 
    @user = User.new(user_params)

    if @user.save
      sign_in @user 
      flash[:success] = "Welcome to the Sample App!"
        redirect_to @user
    else
        render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render "edit"
    end
  end

  private

    def user_params
        params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end
end

Notice how all the creation happens in the controller instead of the model. The model is not supposed to create instances of itself. It is a means for the controller to talk to the database. I would suggest moving your create/strong parameters into the user controller. You may wish to clone your project in case you need to go back.

Michael Hartl's Rails Tutorial might be worth checking out as it builds a similar app to this one but everything is from scratch and uses Rails 4 and strong parameters. I did most of that and came back to this tutorial and just used Rails 3.2 since then it is more simple to learn the basics.

In my project there is no user controller present, there is just the application controller and the status controller. Would that be the source of the problem? if so how would you generate one while still supporting devise? Thanks though for all your help so far! I really appreciate it

Chris Dziewa
Chris Dziewa
17,781 Points

rails generate controller users new create show will create a users controller with the actions: new, create, and show. I do not have a ton of experience yet when it comes to this kind of change. There is a lot more that is required for actually implementing the controller correctly after it's created. If you are absolutely new to Rails I would recommend doing this tutorial in Rails 3.2. It will save you a lot of headaches later. Plus, if you end up working on a project later on, you will be able to handle both older attribute protection methods as well as new ones. Also there is a build a to-do list app which uses Rails 4. I wish I could be of more help.

Yeah I will probably downgrade my version because implementing a new user controller would be one big headache. But thanks anyway for your help, I appreciate it! Hopefully treehouse will update their tutorials soon for rails

Chris Dziewa
Chris Dziewa
17,781 Points

If you are on the Rails track the team has just added an update for the track. This means that they are probably planning on retiring that content from the site soon. You can choose to upgrade. I'm debating whether I want to myself because I'm on the second course for treebook, though I prefer Rspec to shoulda for testing. Also, the levels just feel too much like dictating exercises as opposed to actually learning-another reason an update is much needed.

Yes I feel the same way about some of it's tutorials too.