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

Programming > Build a Simple Ruby on Rails Application > Customizing Forms > Creating Relationships error first_name == nill

Hi Folks,

I'm having some issue with the first_name field . I saw in the forum that other people had the same problem but the all cases the issue was the number the id was wrong.

In my case I know the number the id of the pk field in the User Table, the problem is that the foreing key user_id in the Status table is nill even after I update the form.

I saw the lesson three times and did the steps again but is not working.

What could be.

PS: I'm using rails 4 and the only difference that I noted was the method "attr_accessible" is not accessible when I installed the device gem , so I installed the gem "attr_accessible" in the Gem File.

2 Answers

Can you share your code from statuses_controller.rb?

Hi Alan ,

This is my statuses_controller.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(status_params)

          respond_to do |format|
            if @status.save
              format.html { redirect_to @status, notice: 'Status was successfully created.' }
              format.json { render action: 'show', status: :created, location: @status }
            else
              format.html { render action: '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(status_params)
              format.html { redirect_to @status, notice: 'Status was successfully updated.' }
              format.json { head :no_content }
            else
              format.html { render action: '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 }
            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 status_params
            params.require(:status).permit(:name, :content)
          end
      end

Hi Folks, What can I do to fix this issue?

Thanks

Fabricio

From what I can tell, you're not ever setting up the user relationship on the status. What if you put something like:

@status.user = current_user 

after you create the status, so create would look like:

def create
  @status = Status.new(status_params)
  @status.user = current_user 
  ...

Where exactly would you inject that? I seem to have the same problem, my code is the same as fabricio mattos