pjmanning
pjmanning

Reputation: 1311

Rails Form Submission, User can't be blank

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?

After creating an event the error pops up under 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 (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 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

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 %>

I think I may have edited a path for a link in my 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

debugger

--- !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

debugger for my quick_event_form where the error pops up "-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

FOUND THE CULPRIT!....i want a user to have an event if they rsvp to it. So if i take out "through rsvps" the event gets created...however that doesn't help my rsvp system :(

user.rb

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

Upvotes: 0

Views: 2214

Answers (2)

Musili Alfred M.
Musili Alfred M.

Reputation: 3135

This is how your "new" and "create" actions should be in "events_controller.rb". Remove this line @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

Upvotes: 2

Stone
Stone

Reputation: 2668

Why not initially build the event with the user association in the new action?

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

Upvotes: 0

Related Questions