イベント フォームからイベントを作成しようとすると、「ユーザーを空白にすることはできません」というフォーム エラーが表示され続けます。イベントには、イベントの作成者を示す feed_item を投稿するための user_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
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のデバッガー「-User can't be blank」
--- !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
犯人を見つけた!....私は、ユーザーが rsvp した場合にイベントを開催してもらいたいと思っています。したがって、「rsvpsを介して」取り出すと、イベントが作成されます...しかし、それは私のrsvpシステムには役立ちません:(
user.rb
has_many :rsvps
has_many :events, through: :rsvps, dependent: :destroy