【问题标题】:passing product info from one view to another将产品信息从一个视图传递到另一个视图
【发布时间】:2014-09-03 16:51:00
【问题描述】:

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

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

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>

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

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

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

我应该如何将此产品详细信息传递到显示视图以便看到产品详细信息?

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

最后以下是“显示”视图的部分内容:-

<h2>Individual Product</h2>

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

您目前可以看到的实际上根本不是任何东西,因为我只是在测试将信息传递给它。

以下是 rake 路由的结果:-

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

【问题讨论】:

  • 您没有在链接中传递您的产品,但它应该会给您一个路由错误。你没有收到任何错误?
  • 你能显示你的 routes.rb 文件和你的完整视图吗
  • 嗨,Mandeep,没有路由错误,没有任何类型的错误,而是在单击时将用户直接重定向回“索引”页面。
  • 奇怪的是你没有为你的路由get "store/show"指定控制器和动作名称。你能发布 rake 路由的输出吗?
  • 您好 Mandeep,感谢您抽出宝贵时间查看此内容。我已经在我的帖子中包含了 rake 路由的结果。

标签: ruby-on-rails link-to


【解决方案1】:

您的代码中有几个错误:

一个。让我们看看你的路线,对于你的表演动作,你的路线是:

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

【讨论】:

  • 我真的很抱歉 Mandeep,你的解释似乎有道理,但是我很难理解关于单一资源的文章,所以必须阅读更多内容。我天真地采纳了你的建议,导致了额外的错误,我今天把事情搞砸了!!第一个问题是更改导致与coderoot 'store#index' 发生冲突,如:'store'code 所以我删除了这个,当然商店索引页面也丢失了。然后我意识到,因为您将其命名为 :stores(根据命名约定),所以我必须重命名我的控制器。
  • 现在我似乎无法正确编写任何链接。在商店索引页面上,每个部分都有链接;办公室,写作等。以前的链接写为codestore_office_pathcode 我将其更改为codestores_office_pathcode 这不起作用或许多尝试的组合!对不起,又输了!!我认为任何链接都是通过调用控制器然后调用路径助手中的操作来构建的。我还认为在某些情况下我不需要包含控制器名称。我没有得到什么?
  • @StuartAckland 抱歉,如果这导致您出现错误,但这些错误只会在长期内帮助您,如果您现在在初始阶段解决它们而不是稍后解决您的复数路线问题,您应该结帐@ 987654322@。对于您的路径助手,您始终可以执行 rake 路由并查看它们为您创建的路径,或者您甚至可以使用 :as option 更改这些路径助手
  • @StuartAckland 我改变了你的路线,因为 a。无论如何,它必须更改,因为您没有通过产品 ID 进行展示操作。湾。我只是认为它会重新考虑您的代码。如果您遇到任何错误,那么您可以随时将它们作为问题发布,我很确定来自 SO 社区的人一定能够帮助您解决这些问题:)
  • 非常感谢 Mandeep 提供的所有帮助,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
相关资源
最近更新 更多