用户ID未保存在“事件”表中。这是一个使用postgresql数据库的rails项目

时间:2013-12-27 00:42:02

标签: ruby-on-rails ruby-on-rails-4 strong-parameters

我使用devise来创建我的用户,并且我在创建事件时尝试将user_id保存在事件表中,我不断收到错误"无法找到没有id"的用户。不确定是什么问题是任何指针将不胜感激。

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

这是我的架构

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

1 个答案:

答案 0 :(得分:0)

您是否看过params[:user_id],看看它是否符合您的想法?我非常确定params[:user_id]将是nil

让我们看看您的params处理:

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

require方法将查找params[:event]并在找到后将其返回。然后permit会在:name中查找:event_key:user_id,...,params[:event]。因此,:user_id可能是params[:event][:user_id]而不是params[:user_id]。结果是你说:

@user = User.find(nil)

find并不想听到nil s。

这应该更好:

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

如果@user应该始终是当前用户,那么您希望从current_user而不是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