2013-05-26 107 views
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。

相关问题