使用rails时使用“_path”的未定义方法form_for

问题描述:

我在使用Rails form_for helper时遇到了(我认为)路由错误。我一直在四处搜寻,看着this question,但复数的“static_event”是“static_events”,所以我很茫然。任何帮助将被认识。下面是详细信息....使用rails时使用“_path”的未定义方法form_for

ActionView::Template::Error (undefined method `static_events_path' for #<#<Class:0x007f9fcc48a918>:0x007f9fcc46fa78>): 

我的模型:

class StaticEvent < ActiveRecord::Base 
attr_accessible :content, :title, :discount, :location, :day_of_week, :start_time 

我的控制器:

class StaticEventsController < ApplicationController 

    before_filter :authenticate, :only => [:create, :destroy] 
    before_filter :authorized_user, :only => [:destroy] 


    def new 
    @title = "Share An Event" 
    @static_event = StaticEvent.new 
    end 

    def create 
    @static_event = current_user.static_events.build(params[:event]) 
    if @static_event.save 
     flash[:success] = "Event Shared" 
     redirect_to @static_event #this was the old version 
    else 
     render :new 
    end 
    end 

路线:

match '/static-events/new', :to => 'static_events#new' 
match '/static-events/',  :to => 'static_events#index' 
match '/static-events/:id', :to => 'static_events#show' 

视图

<%= form_for (@static_event) do |f| %> 
<%= render 'shared/error_messages', :object => f.object %> 
<%= text_field "static_event", "title", "size" => 48 %> 
<%= time_select "static_event", "start_time", {:ampm => true, :minute_step => 15} %> 
<%= text_area "static_event", "content", "cols" => 42, "rows" => 5 %> 
<%= text_field "static_event", "discount", "size" => 48 %> 
<%= text_field "static_event", "location", "size" => 48 %> 
<%= text_field "static_event", "day_of_week", "size" => 48 %> 
<input name="" type="submit" class="button" value="share on chalkboard" /> 
<% end %> 

只有使用resources方法创建路由自动命名。

如果你希望把你的路线,使用:as选项:

match '/static-events/new', :to => 'static_events#new', :as => :new_static_event 
match '/static-events/',  :to => 'static_events#index', :as => :static_events 
match '/static-events/:id', :to => 'static_events#show', :as => :static_event 

然而,最好使用resources方法。你必须通过你的模型的“真”的名字作为第一个参数,然后如果你想覆盖的路径:

resources :static_events, :path => 'static-events' 
+0

太好了,我已经更新了它。谢谢Fábio! – Alekx 2012-01-04 06:15:51

+0

在rails 4中,您需要指定http方法以及'via' – courtsimas 2013-10-23 14:48:27

+1

作为一个方面,对于嵌套路由,您必须传递一对值以便'form_for([@ static_event,@ sub_event] )' – 2014-01-29 02:23:03

运行rake routes,你会看到你的路由列表。然后,您可以修复路线文件以具有适当的路线路径。

+0

从我所看到的,我的路线看起来准确。 – Alekx 2012-01-03 02:00:13

+0

好的调试技巧 - 这对我有帮助。 – infl3x 2015-02-26 07:43:38

首先,你应该定义你的路线是这样的:

resources 'static-events', :only => [:new, :create] 

这将创建新的路径,创造方法。

因为当你使用一个新的ActiveRecord对象作为参数来形式化时,它会使用POST动词在你的路径文件中寻找类似于static_events_path的* s_path。

我认为你已经定义你的路线的方式不会创建带有POST动词的static_events_path(你可以通过使用rag路由来检查,就像megas所说的那样)。因此,不要再使用匹配,使用资源或获取/发布/ ...而不是匹配您的Rails 3项目。

编辑

我昨天没注意,但对于创建方法的路由。在static_events#index之前添加以下路线或删除所有路线,并按照上面所述操作。

post '/static-events/', :to => 'static_events#create' 
+0

':static-events'不是一个有效的符号 – 2012-01-03 13:33:18

+0

我用过:static_events,它工作。 – Alekx 2012-01-04 06:14:57

+0

你是正确的注入符号不能包含破折号。我修复了这个错误 – basgys 2012-01-04 11:49:32

这发生在我身上时,我使用的是嵌套的资源,却忘了真正初始化父在cancan中使用load_and_authorize_resource的资源。因此,父资源是空的,它抛出了这个错误。

我通过在控制器的父级上声明load_and_authorize_resource来修复它。