将产品信息从一个视图传递到另一个视图

时间:2014-09-03 16:51:01

标签: ruby-on-rails link-to

我的“办公室”视图显示在产品数据库中找到的办公产品。此视图中显示了多个产品,因此我希望用户能够点击办公产品,该产品进入“展示”视图,仅显示产品详细信息。

我的商店控制器如下所示: -

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

Office视图中的link_to代码如下所示: -

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

show视图中产品的代码如下所示: -

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

办公室产品在办公室视图中正确显示。点击产品link_to后,浏览器会使用action: :index重定向,因为@products.nil?评估为真。

如何将此产品详细信息传递到show视图中,以便查看产品详细信息?

以下是我的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

1 个答案:

答案 0 :(得分:0)

您的代码中有几处错误:

一个。让我们看看你的路线,你的路线是你的路线:

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

对于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,然后您可以在控制器操作中找到它

湾将您的链接更改为:

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

℃。显示行动:

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
相关问题