0

我正在尝试通过事件表单创建一个事件,但我不断收到一个表单错误,上面写着“用户不能为空”。该事件需要一个 user_id 来发布一个 feed_item,显示谁创建了该事件。为什么无法创建此事件?

创建事件后,在 localhost:3000/events 下会弹出错误

事件.rb

class Event < ActiveRecord::Base
  attr_accessible :description, :location, :title, :category_id, :start_date,
  :start_time, :end_date, :end_time, :image

  belongs_to :user
  belongs_to :category

  has_many :rsvps
  has_many :users, through: :rsvps, dependent: :destroy

  mount_uploader :image, ImageUploader

  validates :title, presence: true, length: { maximum: 60 }
  validates :user_id, presence: true

create_events.rb(数据库)

class CreateEvents < ActiveRecord::Migration
  def change
    create_table :events do |t|
      t.string :title
      t.date :start_date
      t.time :start_time
      t.date :end_date
      t.time :end_time
      t.string :location
      t.string :description
      t.integer :category_id
      t.integer :user_id

      t.timestamps
    end
    add_index :events, [:user_id, :created_at]
  end
end

events_controller.rb

def index
  @events = Event.paginate(page: params[:page])

  # For calendar
  @events = Event.all
  @events_by_date = @events.group_by(&:start_date)
  @date = params[:date] ? Date.parse(params[:date]) : Date.today
  # For calendar

end

def new
    @event = Event.new
    @user = current_user
end

def create
    @event = current_user.events.build(params[:event])
    if @event.save
        flash[:success] = "Sesh created!"
        redirect_to root_url
    else
        @feed_items = []
        render 'static_pages/home'
    end
end

路线.rb

SampleApp::Application.routes.draw do

  resources :users do
    member do
      get :following, :followers, :events
    end
  end

  resources :events do
    member do
      get :members
    end
  end

  root to: 'static_pages#home'

事件/new.html.erb

<%= form_for @event, :html => {:multipart => true} do |f| %>
  <%= render 'shared/error_messages', object: @event %>

我想我可能已经在我的 shared/_header.html.erb 中编辑了链接的路径...

<% if signed_in? %>
<%= link_to "Create Sesh", new_event_path, class: "btn btn-small btn-primary" %>
<% end %> 

<li><%= link_to "Events", events_user_path(current_user) %></li>

users_controller.rb

  def events
    # For calendar
    @events = Event.all
    @events_by_date = @events.group_by(&:start_date)
    @date = params[:date] ? Date.parse(params[:date]) : Date.today
    # For calendar

    @title = "Events"
    @event = Event.find(params[:id])
    @event = @event.rsvps.paginate(page: params[:page])
    render 'events/index'
  end

调试器

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
!binary "dXRmOA==": ✓
!binary "YXV0aGVudGljaXR5X3Rva2Vu": 6Nm9KxVpcGCvL7l94ypuPCCs/WL4F9W6zAMKqlSf2jQ=
!binary "ZXZlbnQ=": !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  !binary "dGl0bGU=": Trying to Create Event
  !binary "Y2F0ZWdvcnlfaWQ=": '15'
  !binary "c3RhcnRfZGF0ZQ==": '2012-09-22'
  !binary "ZW5kX2RhdGU=": '2012-09-23'
  !binary "bG9jYXRpb24=": Home
  !binary "ZGVzY3JpcHRpb24=": CREATE DAMMIT!
!binary "Y29tbWl0": Create Sesh!
action: create
controller: events

我的 quick_event_form 的调试器弹出错误“-用户不能为空”

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: 6Nm9KxVpcGCvL7l94ypuPCCs/WL4F9W6zAMKqlSf2jQ=
event: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  title: Make SESHI
  category_id: '16'
  start_date: '2012-09-22'
  location: home
commit: Make a quick Sesh!
action: create
controller: events

找到了罪魁祸首!....我希望用户有一个事件,如果他们回复它。因此,如果我“通过 rsvps”取出事件,则会创建事件......但这对我的 rsvp 系统没有帮助:(

用户.rb

  has_many :rsvps
  has_many :events, through: :rsvps, dependent: :destroy
4

2 回答 2

2

这就是“events_controller.rb”中“新建”和“创建”操作的方式。删除此行@user = current_user

def new
    @event = Event.new
end

def create
    @event = current_user.events.build(params[:event])
    if @event.save
       flash[:success] = "Sesh created!"
       redirect_to root_url
    else
       @feed_items = []
       render 'static_pages/home'
    end
end
于 2012-12-04T21:48:05.480 回答
0

为什么不最初在新操作中使用用户关联来构建事件?

def new
    @event = current_user.events.build
    @user = current_user
end

def create
    @event = Event.build(params[:event])
    if @event.save
        flash[:success] = "Sesh created!"
        redirect_to root_url
    else
        @feed_items = []
        render 'static_pages/home'
    end
end
于 2012-09-22T06:55:15.017 回答