Rails Form Submission, User can't be blank
Posted
by
pmanning
on Stack Overflow
See other posts from Stack Overflow
or by pmanning
Published on 2012-09-22T01:25:20Z
Indexed on
2012/09/22
3:37 UTC
Read the original article
Hit count: 137
ruby-on-rails
|form-submit
I'm trying to create an event through an event form and I keep getting a form error that says "User can't be blank". The event needs a user_id to post a feed_item showing who created the event. Why can't this event get created?
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 (database)
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 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
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 %>
© Stack Overflow or respective owner