我正在尝试设置一种在我的Rails应用中创建新事件的方法。但是,当我转到正确的页面(根据我的路线)以创建新事件时,我收到此错误:
NoMethodError in EventsController#create
undefined method `event_url for #<EventsController:0x000000035a9dc8>
这是相关领域(第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 }
这是我尝试加载页面时服务器日志所说的内容:
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回购:http://github.com/yamilethmedina/wheels_registration
我安装了Ruby on Rails 4,设计和simple_form。任何帮助将不胜感激。
答案 0 :(得分:1)
因为您正试图重定向到'show'操作
23 format.html { redirect_to @event, notice: 'Event was successfully created.' }
但是你的config / routes.rb
中没有行动'show'的路线答案 1 :(得分:1)
当您在Rails中使用redirect_to
对象时,它会尝试将您重定向到该对象的相应show
操作。在这种情况下,使用event
,它会为EventsController#show
尝试路线。
在routes.rb
文件中,您尚未在show
中为EventsController
操作定义路线 - 通过添加,您将能够获得所需的重定向。< / p>
此外,由于您已经定义了create
和index
等其他操作,因此可以使用resources :events
文件中的routes.rb
替换所有操作,因为这会生成所有操作他们是自动的。
答案 2 :(得分:0)
问题是:NoMethodError (undefined method `event_url' for # <EventsController:0x00000004aebd80>)
您是否尝试传递event_url?您的活动表没有event_url。所以修复将是一些事情:
rails g migration AddEventUrlToEvents event_url:string
rake db:migrate
event_url
添加到事件控制器中允许的参数中。