Ruby on rails Rails表单提交,用户可以';不要空白

Ruby on rails Rails表单提交,用户可以';不要空白,ruby-on-rails,form-submit,Ruby On Rails,Form Submit,我试图通过一个事件表单创建一个事件,但我一直收到一个表单错误,上面写着“用户不能为空”。事件需要一个用户id来发布一个提要项,其中显示了事件的创建者。为什么不能创建此事件 创建事件后,在localhost:3000/events下会弹出错误 event.rb class Event < ActiveRecord::Base attr_accessible :description, :location, :title, :category_id, :start_date, :sta

我试图通过一个事件表单创建一个事件,但我一直收到一个表单错误,上面写着“用户不能为空”。事件需要一个用户id来发布一个提要项,其中显示了事件的创建者。为什么不能创建此事件

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

event.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
routes.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'
events/new.html.erb

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

--- !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
“快速事件”窗体的调试器,其中会弹出错误“-用户不能为空”

找到了罪犯!。。。。我希望用户有一个事件,如果他们rsvp到它。因此,如果我取出“通过rsvps”,事件就会被创建…但是这对我的rsvp系统没有帮助:(

user.rb

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

为什么不首先在新操作中使用用户关联构建事件

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
这就是“events\u controller.rb”中“new”和“create”操作的方式。删除此行
@user=current\u 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

请显示
config/routes.rb
文件,以显示您的资源是如何设置的,如果有嵌套的,以及相关的
视图
表单行。我建议您使用调试器调试create action,并验证传递的所有参数。由于imho,您的代码看起来合理。嘿,我刚刚添加了调试器。有点clu我相信可能是路由和用户控制器事件,但我仍然不确定
--- !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
--- !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
  has_many :rsvps
  has_many :events, through: :rsvps, dependent: :destroy
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
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