Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将产品信息从一个视图传递到另一个视图_Ruby On Rails_Link To - Fatal编程技术网

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

Ruby on rails 将产品信息从一个视图传递到另一个视图,ruby-on-rails,link-to,Ruby On Rails,Link To,我的“Office”视图显示在产品数据库中找到的Office产品。此视图中显示多个产品,因此我希望用户能够单击office产品,该产品将转到“显示”视图,该视图仅显示该产品的详细信息 我的商店控制器如下所示:- class StoreController < ApplicationController def index @products = Product.all end def show @products = Product.find_by(:id

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

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

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
<p class="showArticle"><%= link_to 'Show Article', store_show_path %></p>
<%= @products.title(:id) %>
以下是应用程序布局:-

<!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>

代码中有几个错误:

a。让我们看看你的路线,对于你的表演,你的路线是:

store_show      GET     /store/show(.:format)           store#show
对于ShowAction,您没有传递stores id,因此当您尝试在操作中查找它时,它是零

修复

你应该使用。您可以这样做:

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
对于您的表演动作,这将创建如下路线:

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

您没有在链接中传递产品,但它会给您一个路由错误。您没有收到任何错误?您能显示routes.rb文件和完整视图吗?Hi Mandeep,没有路由错误,没有任何类型的错误,而是单击后,用户被直接重定向回“索引”页面。奇怪的是,您没有为您的路由指定控制器和操作名称
获取“存储/显示”
。你能发布rake路由的输出吗?嗨,Mandeep,谢谢你花时间看这个。我已经在我的帖子中包含了rake routes的结果。我真的很抱歉Mandeep,你的解释似乎有道理,但是我正在努力理解关于单一资源的文章,因此必须阅读更多关于这方面的内容。我天真地执行了你的建议,导致了更多的错误,今天我把事情弄得一团糟!!第一个问题是,这些更改导致与
code
root'store#index'发生冲突,如:“store'
code
,因此我删除了此项,当然,store index页面随后丢失。然后我意识到,因为你给它命名:stores(按照命名约定),我不得不重命名我的控制器。现在我似乎无法正确地编写任何链接。在商店索引页面上有指向每个部分的链接;办公室、写作等。以前的链接被写为
code
store\u office\u path
code
我把它改为
code
stores\u office\u path
code
,但这不起作用,或者尝试了很多组合!对不起,我又迷路了!!我认为任何链接都是通过调用控制器然后调用路径帮助器中的操作来构建的。我还认为,在某些情况下,我不需要包括控制器的名称。我没有得到什么?@StuartAckland很抱歉,如果这导致了您的错误,但这些错误只会在长期内对您有所帮助,如果您现在在初始阶段解决这些问题,而不是稍后解决您应该结帐的问题,这会更好。对于您的路径助手,您可以始终执行rake路由,并查看它们为您创建的路径,或者您甚至可以使用它们来更改这些路径helpers@StuartAckland我改变你的路线是因为a。无论如何,它必须改变,因为你没有通过产品id来展示动作。B我只是想它会重新考虑你的代码。如果您有任何错误,那么您可以随时将其作为问题发布,我非常确信来自SO社区的人一定能够帮助您回答这些问题:)非常感谢Mandeep为您提供的所有帮助,非常感谢。
GET /stores/:id(.:format)   stores#show
<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