2014-09-03 269 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

相关问题