2012-01-05 209 views
0

路由時:沒有路由匹配指定 '路徑'

resources :news_items, :path => 'news', :as => 'news' do 
    collection do 
    get 'all' => 'news_items#news' 
    end 

控制器:

def new 
    @news = NewsItem.new 
    @news.file_uploads.build 

    respond_to do |format| 
    format.html # new.html.erb 
    format.json { render json: @news } 
    end 
end 

形式:

<%= form_for(@news, :html => {:class=>'form-stacked', :multipart => true}) do |f| %> 
    <% if @news.errors.any? %> 

當運行耙路由

  all_news_index GET /news/all(.:format)     {:action=>"news", :controller=>"news_items"} 
      news_index GET /news(.:format)      {:action=>"index", :controller=>"news_items"} 
        POST /news(.:format)      {:action=>"create", :controller=>"news_items"} 
      new_news GET /news/new(.:format)     {:action=>"new", :controller=>"news_items"} 
      edit_news GET /news/:id/edit(.:format)   {:action=>"edit", :controller=>"news_items"} 
       news GET /news/:id(.:format)     {:action=>"show", :controller=>"news_items"} 
        PUT /news/:id(.:format)     {:action=>"update", :controller=>"news_items"} 
        DELETE /news/:id(.:format)     {:action=>"destroy", :controller=>"news_items"} 

當我去「/新聞/新」我得到:

No route matches {:path_prefix=>"news", :controller=>"news_items", :format=>nil} 

當我去「/新聞/ 4 /編輯」我得到:

No route matches {:path_prefix=>"news", :action=>"show", :controller=>"news_items", :format=>nil, :id=>#<NewsItem id: 4,... 
+0

什麼是您的控制器調用,並在哪個文件駐留? – meagar 2012-01-05 18:29:11

+0

@meagar NewsItemsController && news_items_controller.rb – LDK 2012-01-05 18:46:22

回答

0

我認爲這個問題是與form_for(@news)一行。

這將創建一個表單,URL由@news類推斷。

看起來這是類NewsItem,所以它會嘗試鏈接到news_items_path

您應該嘗試明確指出網址。

form_for(@news, :url => news_path(@news)) 
+1

因爲我使用新的和編輯的形式,所以這個效果更好::url =>(@ news.id.nil??new_news_path:news_path(@news))。謝謝你的提示! – LDK 2012-01-05 18:58:49

+0

編輯:上面的'new_news_path'應該是'news_index_path' – LDK 2012-01-05 19:12:26