2012-01-03 2 views
29

레일즈 form_for 헬퍼를 사용하는 동안 라우팅 오류가 발생합니다. 나는 주변을 검색하고 this question을 보았지만, pluralize로 "static_event"에 대한 복수형은 "static_events"이므로 손실이 발생했습니다. 어떤 도움도 감사 할 것입니다. 여기에 세부 사항은 ....입니다레일을 사용하는 중에 "_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 메서드를 사용하여 만든 경로 만 자동으로 지정됩니다.

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 방법을 사용하는 것이 좋습니다 :

당신이 당신의 경로 이름을 지정할 경우

:as 옵션을 사용합니다. 그런 다음 원하는 경우 경로를 오버라이드 (override), 첫 번째 매개 변수로 모델의 "true"로 이름을 전달해야합니다 내가 중첩 된 자원을 사용했을 때이 나에게 무슨 일이 있었

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

위대한, 이걸 업데이 트했습니다. 감사 파비 오! – Alekx

+0

레일 4에서 http 메서드를 'via'로 지정해야합니다 – courtsimas

+1

중첩 된 경로의 경우 한 쌍의 값을 전달해야하므로 'form_for ([@ static_event, @ sub_event])' –

3

실행 rake routes 당신은 당신의 경로의 목록이 표시됩니다. 그런 다음 적절한 경로 경로를 갖도록 경로 파일을 수정할 수 있습니다.

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

이 새에 대한 경로를 생성 및 방법을 만들 것입니다 : 모든

+0

내가 볼 수있는 것부터 내 경로가 정확 해 보입니다. – Alekx

+0

좋은 디버깅 팁 - 이것이 도움이되었습니다. – infl3x

8

먼저, 당신은 당신의 경로 이런 식으로 정의해야합니다.

for에 대한 인수로 새 ActiveRecord 객체를 사용하면 POST 동사가있는 route 파일에서 static_events_path와 같은 * s_path가 검색됩니다.

라우트를 정의한 방식으로 POST 동사와 함께 static_events_path가 작성되지는 않는다고 생각합니다 (megas가 말한 것처럼 레이크 라우트를 사용하여 확인할 수 있습니다). 그래서 더 이상 match를 사용하지 말고, 리소스를 사용하거나, Rails 3 프로젝트에서 match 대신에/post/...를 얻으십시오.

편집

어제 통지를하지 않았지만, 방법을 만들에 대한 경로가 없습니다. 아래의 경로를 static_events # index 앞에 추가하거나 모든 경로를 삭제하고 위와 같이하십시오.

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

': static-events'는 유효한 심볼이 아닙니다 –

+0

static_events를 사용했습니다. – Alekx

+0

당신은 바로 injekt이야, 기호는 대시를 포함 할 수 없습니다. 나는 실수를 고쳤다. – basgys

1

을하지만 실제로 부모를 초기화하는 것을 잊었다 cancan에서 load_and_authorize_resource을 사용하는 리소스. 따라서 부모 리소스가 null이며이 오류가 발생했습니다.

컨트롤러의 부모에 load_and_authorize_resource을 선언하여 문제를 해결했습니다.

관련 문제