2014-09-03 263 views
1

我的「辦公室」視圖顯示在產品數據庫中找到的辦公室產品。此視圖中顯示了多個產品,因此我希望用戶能夠點擊辦公室產品,然後轉到顯示該產品詳情的「展示」視圖。將產品信息從一個視圖傳遞到另一個視圖

我的存儲控制器是這樣的: -

class StoreController < ApplicationController 
    def index 
    @products = Product.all 
    end 

    def show 
    @products = Product.find_by(:id) 
    if @products.nil? 
     redirect_to action: :index 
    end 
    end 
end 

辦公室視圖內的link_to代碼如下所示: -

<p class="showArticle"><%= link_to 'Show Article', store_show_path %></p> 

爲在Show View產品的代碼看起來是這樣的: -

<%= @products.title(:id) %> 

辦公室產品在辦公室視圖中正確顯示。當點擊產品link_to時,瀏覽器使用action: :index重定向,因爲@products.nil?被評估爲true。

我應該如何將這些產品的詳細信息傳遞到show view以便看到產品細節?

以下是我的routes.rb文件: -

Easygifts::Application.routes.draw do 
    get "store/index" 
    get "store/writing" 
    get "store/office" 
    get "store/time" 
    get "store/home" 
    get "store/wellness" 
    get "store/travel" 
    get "store/bags" 
    get "store/leisure" 
    get "store/show" 
    resources :products 

    # The priority is based upon order of creation: first created -> highest priority. 
    # See how all your routes lay out with "rake routes". 

    # You can have the root of your site routed with "root" 
    root 'store#index', as: 'store' 

    # Example of regular route: 
    # get 'products/:id' => 'catalog#view' 

    # Example of named route that can be invoked with purchase_url(id: product.id) 
    # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase 

    # Example resource route (maps HTTP verbs to controller actions automatically): 
    # resources :products 

    # Example resource route with options: 
    # resources :products do 
    #  member do 
    #  get 'short' 
    #  post 'toggle' 
    #  end 
    # 
    #  collection do 
    #  get 'sold' 
    #  end 
    # end 

    # Example resource route with sub-resources: 
    # resources :products do 
    #  resources :comments, :sales 
    #  resource :seller 
    # end 

    # Example resource route with more complex sub-resources: 
    # resources :products do 
    #  resources :comments 
    #  resources :sales do 
    #  get 'recent', on: :collection 
    #  end 
    # end 

    # Example resource route with concerns: 
    # concern :toggleable do 
    #  post 'toggle' 
    # end 
    # resources :posts, concerns: :toggleable 
    # resources :photos, concerns: :toggleable 

    # Example resource route within a namespace: 
    # namespace :admin do 
    #  # Directs /admin/products/* to Admin::ProductsController 
    #  # (app/controllers/admin/products_controller.rb) 
    #  resources :products 
    # end 
end 

以下是應用程序佈局: -

<!DOCTYPE html> 
<html> 
<head> 
    <title>Easy Gifts UK Ltd - Home of promotional gifts</title> 
    <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> 
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 
    <%= csrf_meta_tags %> 
</head> 
<body class='<%= controller.controller_name %>'> 
<div id="wrapper"> 

    <div id="branding"> 
    <%=link_to(image_tag("easyGiftsLogo.jpg", :width => "210", :height => "70", :alt => "Easy Gifts UK Logo"), store_path) %> 
     <div id="search2"> 
      <p>search field</p> 
     </div> 
    </div> 

    <div id="content"> 
     <%= yield %> 
    </div> 

    <div id="mainNav"> 
     <ul> 
      <li><%= link_to_unless_current('Writing', { action: 'writing' }) %></li> 
      <li><%= link_to_unless_current('Office', { action: 'office' }) %></li> 
      <li><%= link_to_unless_current('Time', { action: 'time'}) %></li> 
      <li><%= link_to_unless_current('Home', { action: 'home'}) %></li> 
      <li><%= link_to_unless_current('Wellness', {action: 'wellness'}) %></li> 
      <li><%= link_to_unless_current('Travel', {action: 'travel'}) %></li> 
      <li><%= link_to_unless_current('Bags', {action: 'bags'}) %></li> 
      <li><%= link_to_unless_current('Leisure', {action: 'leisure'}) %></li>   
     </ul> 
    </div> 
    <div id="footer"> 
     <ul> 
      <li><%= link_to 'Admin', products_path %></li> 
      <li><a href="#">link 2</a></li> 
      <li><a href="#">link 3</a></li> 
     </ul> 
    </div> 
</div> 
</body> 
</html> 

而下面是部分爲 '辦公室' -

<%= image_tag("office (1).jpg", :class => "imgBorder", :width => "808", :height =>"228", :alt => "Office section - Easy Gifts UK Ltd") %> 
      <%= render "notice" %> 
      <% @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_show_path %></p> 
        <p class="addTo"><a href="#">Quote this item</a></p> 
       </div> 
      <% end %> 
      <p class="clear"><%= will_paginate @products %></p> 

最後以下是'show'視圖的部分內容: -

<h2>Individual Product</h2> 

<%= @products.title(:id) %> 

你現在看到的確實不是什麼,因爲我只是測試獲取信息。

以下是耙路線的結果: -

c:\Sites\work\easygifts>rake routes 
Prefix   Verb URI Pattern      Controller#Action 
store_index  GET  /store/index(.:format)   store#index 
store_writing GET  /store/writing(.:format)  store#writing 
store_office GET  /store/office(.:format)   store#office 
store_time  GET  /store/time(.:format)   store#time 
store_home  GET  /store/home(.:format)   store#home 
store_wellness GET  /store/wellness(.:format)  store#wellness 
store_travel GET  /store/travel(.:format)   store#travel 
store_bags  GET  /store/bags(.:format)   store#bags 
store_leisure GET  /store/leisure(.:format)  store#leisure 
store_show  GET  /store/show(.:format)   store#show 
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 
store   GET /        store#index 
+0

您沒有在鏈接中傳遞您的產品,但它應該會給您一個路由錯誤。你沒有得到任何錯誤? – Mandeep 2014-09-03 17:01:26

+0

你可以顯示你的routes.rb文件和你的完整視圖 – Mandeep 2014-09-03 17:07:07

+0

嗨Mandeep,沒有路由錯誤,沒有任何錯誤,而不是點擊用戶被重定向直接返回'索引'頁面。 – 2014-09-04 09:27:46

回答

0

有夫婦在你的代碼錯誤:

一個。讓我們看一下你的路線,爲您呈現的行動路線是:

store_show  GET  /store/show(.:format)   store#show 

作秀動作你是不是傳遞商店ID,所以它是零,當你試圖找到它在你的行動。

修復

您應該使用rails resourceful routing。你可以做這樣的事情:

resources :stores do 
    collection do 
    get "writing" 
    get "office" 
    get "time" 
    get "home" 
    get "wellness" 
    get "travel" 
    get "bags" 
    get "leisure" 
    end 
end 

爲您呈現的行動,這將創建一個這樣的路線:

GET /stores/:id(.:format) stores#show 

這將允許您通過您的商店ID在你的鏈接,然後你可以找到它在你的控制器動作

b。將您的鏈接更改爲:

<p class="showArticle"><%= link_to 'Show Article', store_path(office) %></p> // notice we are now passing your product in path helper 

c。顯示操作:

def show 
    @products = Product.find(params[:id]) # notice we have to use params[:id] to find your product also by default find searches your record with id so you don't need to use find_by 
    if @products.nil? 
    redirect_to action: :index 
    end 
end 
+0

我真的很抱歉曼德普,你的解釋似乎是有道理的,但是我很難理解關於奇異資源的文章,所以必須閱讀更多。 我天真地執行了你的建議,導致了額外的錯誤,我今天一直在做一個正確的混亂的東西!第一個問題是,這些更改導致與'code'root'store#index'發生衝突,如'store''code',所以我刪除了這個,當然商店索引頁面丟失了。然後我意識到,因爲你命名它:商店(按照命名約定)我不得不重命名我的控制器。 – 2014-09-05 17:31:40

+0

現在我似乎無法正確寫入我的任何鏈接。在商店索引頁面上有每個部分的鏈接;辦公室,寫作等。以前的鏈接已寫成'code'store_office_path'code'我將其更改爲'code'stores_office_path'code',這不起作用或許多嘗試組合!我很抱歉,但又輸了!我認爲任何鏈接都是通過調用控制器然後在路徑幫助器中執行動作來構建的。我也認爲在某些情況下,我不需要包含控制器名稱。我沒有得到什麼? – 2014-09-05 17:37:24

+0

@StuartAckland對不起,如果這會導致你的錯誤,但這些錯誤只會幫助你長期,如果你現在在初始階段解決它們,而不是稍後爲你的複數路線問題,你應該結帳http://stackoverflow.com/問題/ 646951 /奇異或複數的控制器和輔助性的名字 - 在護欄。對於你的路徑幫助者,你總是可以做耙路徑,看看他們爲你做了什麼路徑,或者甚至可以使用[':as option'](http://guides.rubyonrails.org/routing.html#using-as-嵌套資源)來改變那些路徑助手 – Mandeep 2014-09-05 17:49:19

相關問題