0

我不明白在routes.rb中Rails爲什麼路由錯誤?沒有路由匹配

我在promotions_controller.rb我有加寫

match 'promotions/:id/purchase' => 'promotions#purchase', :as => :purchase_promo 

def purchase 
    @promotion = Promotion.find(params[:id]) 
    respond_to do |format| 
    format.html # purchase.html.erb 
    format.json { render json: @promotion } 
    end 
    end 

,並在視圖促銷活動創建file purchase.erb.html

<div data-role="page" id="acquisto"> 

    <div data-role="header" data-theme="e"> 
    <h1>Purchase?</h1> 
    </div><!-- /header --> 

    <div data-role="content" data-theme="d"> 
    <h4>This promo costs <%= @promotion.price %> .</h4> 
    <p>bla bla bla bla bla bla bla bla bla.</p> 
    <a href="index.html" data-role="button" data-rel="back" data-theme="b">Purchase  album</a> 
    <a href="index.html" data-role="button" data-rel="back">No thanks</a> 

    </div> 

    </div><!-- /page --> 

and in aoth呃頁面寫

<%= link_to 'Buy Promo',:purchase_promo ,'data-rel'=>'dialog',' data-transition'=>'slideup' %> 

有什麼不對?

我得到的路由錯誤

沒有路由匹配{:控制器=> 「促銷」,:動作=> 「購買」}

+0

你的'routes.rb'裏面有'resources:promotions'行嗎? – lurker

+0

是的,我已經有routes.rb – javierZanetti

+0

的資源:促銷我認爲它應該是'<%= link_to'購買促銷',purchase_promo_path,...' – flyingjamus

回答

2

您所創建的路線需要一個ID。鏈接到它,使用:

link_to 'Buy Promo', purchase_promo_path(some_id) 

它不是立即清楚,我正確的ID源是什麼,但根據您所提供的代碼,你可能想:

<%= link_to 'Buy Promo', purchase_promo_path(@promotion) ,'data-rel'=>'dialog',' data-transition'=>'slideup' %> 

你可以還建立這樣的路線通過類似:

resources :promotions do 
    member do 
    post :purchase 
    end 
end 

,將創建正常的收藏級和會員級的路由(indexshow等),同時還CR使用POST方法(您可能希望啓動購買時使用RESTful)爲purchase額外添加成員級路由。在這兩種情況下,您都需要提供ID並使用_path助手來獲取URL。