2014-09-19 86 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

相關問題