I’m just starting out with RoR and have been having a great time so far. Currently I’m trying to post events to a central feed and perhaps I have envisioned how I’m setting things up incorrectly. I have two models, Users and Events, I’ll give a gist. Users have_many events, while Events belong_to Users. I’m trying to save events to a central feed “ala facebook” that is visible by all Users. My Create Method in the Event controller is:
def create
@event = current_user.events.build(params[:event])
if @event.save
flash[:success] = "Event Shared"
render :action => :show
else
render :new
end
end
render :action => :show is diplaying the post and going to /events but the post is not being saved. And when I go back to /events I get the error: Couldn’t find Event without an ID.
Any help would be greatly appreciated. Thanks in advance, the rails community has been great!
I have checked your source code and see currently the following things I don’t think are correct. If they are responsible for the not-storing of events, I do not know:
Your routes.rb file has some things I don’t understand:
If events
belongs_tousers, and users has many events, events should stay in the context of users. See nested resources in the Rails Guides. Your definition for urls there could beYour additional match rule
match '/events', :to => 'events#show'is dangerous, because it overwrites the normal index route. Where will the id of the event you want to show come from?File
events_controller.rbEventsController#createmethod, where does thatcurrent_usercomes from?in your path
if @event.saveyou do at the endrender :action => :show. This willNormal usage here is, that you use
redirect_to @eventwhich will use the correct action and show the correct view.I will not go any further, but I would change these two files first. Are you sure that your
@eventwas not saved? How about addingputsstatements in the path, like that: