Reputation: 670
i'm trying to set up a way to create new events in my Rails app. However, when I go to the correct page (according to my routes) in order to make a new event, I get this error:
NoMethodError in EventsController#create
undefined method `event_url for #<EventsController:0x000000035a9dc8>
This is the relevant area (line 23):
21 respond_to do |format|
22 if @event.save
23 format.html { redirect_to @event, notice: 'Event was successfully created.' }
24 format.json { render :show, event: :created, location: @event }
else
25 format.html { render :new }
This is what my server log says when I try to load the page:
Started GET "/create_event" for 12.43.117.2 at 2015-04-17 15:59:57 +0000
Processing by EventsController#create as HTML
(16.5ms) begin transaction
SQL (0.6ms) INSERT INTO "events" DEFAULT VALUES
(27.1ms) commit transaction
Redirected to
Completed 500 Internal Server Error in 503ms
NoMethodError (undefined method `event_url' for # <EventsController:0x00000004aebd80>):
app/controllers/events_controller.rb:23:in `block (2 levels) in create'
app/controllers/events_controller.rb:21:in `create'
Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack- 4.1.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.5ms)
Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.7ms)
Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (10.7ms)
Rendered /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (155.6ms)
Github repo: http://github.com/yamilethmedina/wheels_registration
I have Ruby on Rails 4, devise, and simple_form installed. Any help would be much appreciated.
Upvotes: 1
Views: 907
Reputation: 839
When you use redirect_to
with an object in Rails, it will attempt to redirect you to the appropriate show
action for that object. In this case, with an event
it would try a route for EventsController#show
.
In your routes.rb
file, you have not defined a route for the show
action in EventsController
– by adding that, you will be able to get the redirect that you want.
Also, since you already have the other actions like create
and index
defined, you can replace them all with resources :events
in your routes.rb
file as that will generate all of them automatically.
Upvotes: 1
Reputation: 445
Because you're trying to redirect to the 'show' action
23 format.html { redirect_to @event, notice: 'Event was successfully created.' }
But there is no route to action 'show' in your config/routes.rb
Upvotes: 1
Reputation: 786
This is the issue: NoMethodError (undefined method `event_url' for # <EventsController:0x00000004aebd80>)
Are you trying to pass in an event_url? Your events table doesn't have an event_url. So the fix would be a few things:
rails g migration AddEventUrlToEvents event_url:string
rake db:migrate
event_url
to your permitted params in your events controller.Upvotes: 0