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 Adding a Dropdown

User ID not saved in statuses

I have a problem. The selected user is not saved in statuses. In status.rb, I have belongs_to :user and in user.rb I have has_many :statuses Dropdown shows users. But whenever, I save, the user is not saved.

I'm using rails 4...don't know if it has anything to do with the problem.

4 Answers

Naomi Freeman
STAFF
Naomi Freeman
Treehouse Guest Teacher

Are you trying to put user or user_id or profile_name? Whatever it is that you're putting, you'll have to add it into the list in the brackets after .permit

Thank you !!!! It worked !

Naomi Freeman
STAFF
Naomi Freeman
Treehouse Guest Teacher

Just keep pushing through this and ignore that for a little bit :) You move away from the dropdown eventually anyways.

I know it's frustrating, but you don't end up having the dropdown anyways. It automatically posts when you post a status. It's coming soon.

Sometimes I've found that's the easiest way to jump the bugs in this tutorial. If it hasn't been solved by the next unit though, then I get worried.

Glenn Harris
Glenn Harris
7,101 Points

Eriko Kawano can you post the contents of your statuses controller? statuses_controller.rb

I haven't touched statuses_controller.rb at all. But here it is. Thanks for your help.

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

Glenn Harris
Glenn Harris
7,101 Points

In addition to what Naomi Freeman said... your create method reads

def create
Status.new(status_params)
blah blah...
end

It needs to read something like

def create
current_user.statuses.new(status_params)
end

You need to start the chain with user first, then call statuses, then call new. The way it's written now just creates a new instance of Status and will leave the user field blank.