2012-01-03 66 views
29

我在使用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 %> 

回答

24

只有使用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

3

運行rake routes,你會看到你的路由列表。然後,您可以修復路線文件以具有適當的路線路徑。

+0

從我所看到的,我的路線看起來準確。 – Alekx 2012-01-03 02:00:13

+0

好的調試技巧 - 這對我有幫助。 – infl3x 2015-02-26 07:43:38

8

首先,你應該定義你的路線是這樣的:

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

1

這發生在我身上時,我使用的是嵌套的資源,卻忘了真正初始化父在cancan中使用load_and_authorize_resource的資源。因此,父資源是空的,它拋出了這個錯誤。

我通過在控制器的父級上聲明load_and_authorize_resource來修復它。

相關問題