2014-09-19 118 views
1

一个id如果我向您展示了我的路线开始了这个问题: -传递轨道4,5

c:\Sites\work\easygifts>rake routes 
     Prefix Verb URI Pattern      Controller#Action 
writing_stores GET  /stores/writing(.:format)  stores#writing 
    office_stores GET  /stores/office(.:format)   stores#office 
    time_stores GET  /stores/time(.:format)   stores#time 
    home_stores GET  /stores/home(.:format)   stores#home 
wellness_stores GET  /stores/wellness(.:format)  stores#wellness 
    travel_stores GET  /stores/travel(.:format)   stores#travel 
    bags_stores GET  /stores/bags(.:format)   stores#bags 
leisure_stores GET  /stores/leisure(.:format)  stores#leisure 
    quote_stores GET  /stores/quote(.:format)   stores#quote 
     stores GET  /stores(.:format)    stores#index 
       POST /stores(.:format)    stores#create 
     new_store GET  /stores/new(.:format)   stores#new 
    edit_store GET  /stores/:id/edit(.:format)  stores#edit 
      store GET  /stores/:id(.:format)   stores#show 
       PATCH /stores/:id(.:format)   stores#update 
       PUT  /stores/:id(.:format)   stores#update 
       DELETE /stores/:id(.:format)   stores#destroy 
     products GET  /products(.:format)    products#index 
       POST /products(.:format)    products#create 
    new_product GET  /products/new(.:format)   products#new 
    edit_product GET  /products/:id/edit(.:format)  products#edit 
     product GET  /products/:id(.:format)   products#show 
       PATCH /products/:id(.:format)   products#update 
       PUT  /products/:id(.:format)   products#update 
       DELETE /products/:id(.:format)   products#destroy 
      root GET /        stores#index 

我有越来越的问题:身份证进入“报价”视图。

我想在我的路线中看到; quote_stores GET /stores/quote/:id(.:format)store#quote或者类似的东西。

可以:id只能通过CRUD传递??我以为我可以通过任何地方传递实例变量,所以我在我的视图中将它作为链接到视图的链接传递给它:id信息。然而,它的ID:

<% @products.each do |office| %> 
     <div class="item"> 
      <%= link_to image_tag(office.image_url), image_path(office.image_url), class: 'fancybox' %> 
      <p><strong><%= office.item_code%></strong> 
      </br><em><%= truncate(office.title, length: 18) %></em></p>     
      <p class="showArticle"><%= link_to 'Show Article', store_path(office) %></p> 
      <p class="addTo"><%= link_to 'Price Info', quote_stores_path(office) %></p> 
     </div> 
    <% end %> 

我这里指的是<%=的link_to“价格信息”,quote_stores_path(办公室)%>这在点击带您到正确的观点,并在URI路径甚至列出了正确的不会传递到:id的信息。

我的控制器代码如下: -

class StoresController < ApplicationController 
    add_breadcrumb 'home', :stores_path 


    def index 
    @products = Product.all 
    end 

    def show 
    @products = Product.find(params[:id]) 
     if @products.nil? 
     redirect_to action: :index 
     end 
    add_breadcrumb 'Back', @products.section 
    end 

    def writing 
    @products = Product.where(:section => 'writing').paginate(:per_page => 5, :page => params[:page]) 
    add_breadcrumb 'writing', writing_stores_path 
    end 

    def office 
    @products = Product.where(:section => 'office').paginate(:per_page => 5, :page => params[:page]) 
    add_breadcrumb 'office', office_stores_path 
    end 

    def time 
    @products = Product.where(:section => 'time').paginate(:per_page => 5, :page => params[:page]) 
    add_breadcrumb 'time', time_stores_path 
    end 

    def home 
    @products = Product.where(:section => 'home').paginate(:per_page => 5, :page => params[:page]) 
    add_breadcrumb 'home', home_stores_path 
    end 

    def wellness 
    @products = Product.where(:section => 'wellness').paginate(:per_page => 5, :page => params[:page]) 
    add_breadcrumb 'wellness', wellness_stores_path 
    end 

    def travel 
    @products = Product.where(:section => 'travel').paginate(:per_page => 5, :page => params[:page]) 
    add_breadcrumb 'travel', travel_stores_path 
    end 

    def bags 
    @products = Product.where(:section => 'bags').paginate(:per_page => 5, :page => params[:page]) 
    add_breadcrumb 'bags', bags_stores_path 
    end 

    def leisure 
    @products = Product.where(:section => 'leisure').paginate(:per_page => 5, :page => params[:page]) 
    add_breadcrumb 'leisure', leisure_stores_path 
    end 

    def quote 
     @products = Product.find_by(params[:id]) 
     if @products.nil? 
     redirect_to action: :index 
     end 
    end 
end 

所以除了我的代码不是干的,什么是我在这里失去了吗?我不明白什么:id的?

回答

0

在你config/routes.rb,你可能有这样一行:

resources :stores 

它创建为stores资源的标准CRUD操作路线。您可以为此资源定义其他操作,这些操作适用于集合(多个产品)或单个成员(单个产品)。

请注意,它可能会更符合逻辑名称的资源products,而不是stores,因为它似乎是处理Products

在你的情况,你要定义一个额外的成员行动。因为它适用于单一的产品,Rails会限定它接受一个id参数的路由:

resources :stores do 
    member do 
    get 'quote' 
    end 
end 

这将产生以下的路径(rake routes):

quote_store GET /stores/:id/quote(.:format)  stores#quote 

注意的路由被称为quote_store,单数形式而非复数形式。

另请参阅the Rails guides for more information about collection and member routes

+0

五位数,我没有听说过这个成员动作,感谢你的资源链接和解决问题的非常明确的答案,非常感谢。 – 2014-09-20 22:17:10

+0

不客气! – fivedigit 2014-09-21 05:24:21