0

I used devise to create my users and I am trying to get the user_id to save in the events table when I create an event, I keep getting the error "cant find user without id". Not sure what the problem is any pointers would be greatly appreciated.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_presence_of :email
  validates_presence_of :encrypted_password
  validates_presence_of :sign_in_count
  has_many :events
end


class Event < ActiveRecord::Base
  validates_presence_of :name
  validates_presence_of :description
  belongs_to :user
  has_many :reviews
end


class EventsController < ApplicationController

  def index
    @events = Event.all
  end

  def new
    @event = Event.new
  end

  def create
    @user = User.find(params[:user_id])
    @event = @user.events.build(event_params)
  end

  def show
    @event = Event.find(params[:id])
  end

  protected

  def event_params
    params.require(:event).permit(:name, :event_key, :location, :date, :event_url, :description, :time,
      :event, :user_id)
  end

end

Here is my schema

ActiveRecord::Schema.define(version: 20131226220750) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "events", force: true do |t|
    t.string   "name",        null: false
    t.string   "location"
    t.string   "event_url"
    t.string   "description", null: false
    t.integer  "user_id"
    t.datetime "time"
    t.datetime "event_date"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "event_key",   null: false
  end

  add_index "events", ["event_key"], name: "index_events_on_event_key", unique: true, using: :btree

  create_table "reviews", force: true do |t|
    t.string   "feedback_comments"
    t.integer  "rating",            null: false
    t.integer  "event_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "first_name",                          null: false
    t.string   "last_name",                           null: false
    t.string   "twitter_handle"
    t.string   "linked_in_url"
    t.string   "phone_number"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

end
mu is too short
  • 426,620
  • 70
  • 833
  • 800
Steve_D
  • 545
  • 2
  • 11
  • 20

1 Answers1

0

Have you looked at params[:user_id] to see if it is what you think it is? I'm pretty sure params[:user_id] will be nil.

Let us look at your params handling:

def event_params params.require(:event).permit(:name, :event_key, :location, :date, :event_url, :description, :time, :event, :user_id) end

The require method will look for params[:event] and return it if found. Then permit will look for :name, :event_key, ..., :user_id within params[:event]. So your :user_id is probably params[:event][:user_id] rather than params[:user_id]. The result is that you're saying:

@user = User.find(nil)

and find doesn't want to hear about nils.

This should work better:

def create
  event  = event_params
  @user  = User.find(event[:user_id])
  @event = @user.events.build(event)
end

If @user is supposed to be the current user all the time then you'd want to get that out of current_user rather than params:

def create
  @user  = current_user
  @event = @user.events.build(event_params)
end

#...

def event_params
  params.require(:event).permit(:name, :event_key, :location, :date, :event_url, :description, :time, :event)
  # No more :user_id up there
end
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • still getting the "couldn't find user without id"... for some reason user_id is not even getting into my event params when it gets to the User.find... event_params => {"name"=>"dsfsdf", "event_key"=>"2232", "location"=>"", "event_url"=>"", "description"=>"dsfsdf sdfsdf", "time"=>""} – Steve_D Dec 27 '13 at 02:09
  • So there's no `:user_id` inside your event. Where does that data come from? What are you expecting to put a `:user_id` in `params[:event]`? – mu is too short Dec 27 '13 at 02:44
  • the user_id is coming from the User object, which is created when a user logs-in. The user object has a has may relationship with events. Not sure what you mean by the second part of the question. – Steve_D Dec 27 '13 at 03:02
  • So is the `:user_id` supposed to be the current user then? – mu is too short Dec 27 '13 at 03:05
  • Then you'd probably want to use `@user = current_user` (as in my update) rather than go rooting around inside `params` for it. – mu is too short Dec 27 '13 at 03:27
  • ok cool i got it. Thanks so much for all your help. I was unaware of the current_user functionality. – Steve_D Dec 27 '13 at 03:44