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

If I put <%= status.user.first_name %> into show.html.erb file like in a video it gives me null. I can't fix it

In the video Jim is teaching how to add Name to the build a simple ruby app. I did everything step by step from the video, I added <%= @status.user.first_name %> to the show.html.erb and same but without @ to the index.html.erb and it gives me the error of undefined, null. I did delete manually all statuses like it mentioned in this video but it did not solve a problem. When I delete these two lines, it works again but name of course is not showing. I also noticed that I don't have attr_accessible in my models status.rb or user.rb and if I add it it complains too.

9 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

Yeah I remembered that they use devise in that set of videos. So the issue here is that these videos are Rails 3, and you are using Rails 4, which introduced strong parameters for security reasons.

Someone else has posted a good workaround:

https://teamtreehouse.com/forum/attraccessible-missing-in-rails-4-heres-the-workaround-for-the-treebook-tutorial

Brandon Barrette
Brandon Barrette
20,485 Points

So what's happening is that the user_id is not being saved in the database for your status.

To fix this, in your statuses_controller, make sure in the statuses_params, you have user_id. So at the bottom of statuses controller, you should have:

private
def statuses_params
    params.require(:status).permit(:user_id, :body) #i don't remember in the video what the status column is called, I put body here
end

Then in your create and update methods, you should see:

def create
     @status = Status.new(statuses_params)
     # I'm not finishing the entire method here..... just this line
end

Let me know how that goes, if that doesn't help, please paste in the code of your status.rb file and statuses_controller.rb file.

Hi Brandon!

Thank you for your help. So, I changed in the statuses_param (:user) for (:user_id, :content) . It solved a problem with show.html.erb, however it still gives the same error when I add a ruby code to the index.html.erb and when I define the label_method: :first name in the form, it does not show anything. However if I change :first name to :email, it shows email.

I was thinking it is a problem in the schema with default method and null. But I'm not sure.

Here are my statuses_controller.rb and status.rb

class StatusesController < ApplicationController
  before_action :set_status, only: [:show, :edit, :update, :destroy]

  # GET /statuses
  # GET /statuses.json
  def index
    @statuses = Status.all
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
  end

  # GET /statuses/new
  def new
    @status = Status.new
  end

  # GET /statuses/1/edit
  def edit
  end

  # POST /statuses
  # POST /statuses.json
  def create
    @status = Status.new(statuses_params)

    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render :show, status: :created, location: @status }
      else
        format.html { render :new }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /statuses/1
  # PATCH/PUT /statuses/1.json
  def update
    respond_to do |format|
      if @status.update(statuses_params)
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { render :show, status: :ok, location: @status }
      else
        format.html { render :edit }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /statuses/1
  # DELETE /statuses/1.json
  def destroy
    @status.destroy
    respond_to do |format|
      format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_status
      @status = Status.find(params[:id])
    end

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

and here is a status.rb

class Status < ActiveRecord::Base
belongs_to :user

end
Brandon Barrette
Brandon Barrette
20,485 Points

Well, this is actually easy to fix. Let's think for a second what's happening. Your user is being saved to the status now. However, it appears the first name is not. This means it's an error happening with the user and not the status. Let's go to the users_controller. Check the users_params, should look something like:

    private
    def user_params
        params.require(:user).permit(:first_name, :last_name, :profile_name) 
        #you might need to add more to permit, depends what your columns are called.
    end

You will need to edit your users by either dumping all users and adding them again. Or manually adding them in rails console. You could also edit them all in the user form to make sure they all have the proper values.

Let me know if that worked or not.

Brandon, I checked but I don't have users_controller. I have user.rb which is in the same folder as a status.rb. In my controllers folder are only statuses_controller and an application_controller. I deleted all users manually. I tried to add your suggested code into user.rb - it did not solve a problem.

Hi Brandon, I followed the link and it works now. Thanks a lot! I think Treehouse should modify a video and let people know about this issue. :)

Brandon Barrette
Brandon Barrette
20,485 Points

Glad you got it to work. The ODOT project uses rails 4, and you build your own users controller instead of using devise (which I found a lot more helpful to understand exactly what's going on). I too wish they would at least put a disclaimer on the Treebook videos.

Happy coding!

Luke Thomas
Luke Thomas
9,077 Points

What link did you follow? I was following along with this as I think I have the same problem, but I got lost at the same spot (before you said "I followed the link..."). What file do I add this into?

Brandon Barrette
Brandon Barrette
20,485 Points

Look at the "Best Answer" and read the link provided there. Someone did step by step instructions to fix devise for rails 4.

Hi Luke,

I changed in my statuses_controller.rb at the bottom from:

def status_params
params.require(:status).permit(:user, :content)
end

to

def status_params
params.require(:status).permit(:user_id, :content)
end

Please note that Brandon suggested that after def should be statuses_params in plural but in my case it did not work, so I changed it back to the status_params

This should work for your show.html.erb file but as soon as you change index file it will not work again.

Then I added registration_controller.rb into controllers folder and add the code:

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

Then it started to work! :)

Alex Romanillos
Alex Romanillos
16,144 Points

For me the problem occurred when calling the <%= status.user.full_name %>

If you are in the same situation, most likely you already fixed the parameter access in the last chapter.

So the solution was that I was following the video, and used the user id= "1". But what I didn't realised is that my users table in the database had my user row with ID = 5. Therefore the Null error.

So go to the rails console, do u = User.all and see which ID you have in your database. This might fix the problem for you as well.

Prabhakar 55
Prabhakar 55
476 Points

This is what fixed my problem. Awesome insight!!!!!