如果我通过向您展示我的路线来开始这个问题: -
c:\Sites\work\easygifts>rake routes
Prefix Verb URI Pattern Controller#Action
writing_stores GET /stores/writing(.:format) stores#writing
office_stores GET /stores/office(.:format) stores#office
time_stores GET /stores/time(.:format) stores#time
home_stores GET /stores/home(.:format) stores#home
wellness_stores GET /stores/wellness(.:format) stores#wellness
travel_stores GET /stores/travel(.:format) stores#travel
bags_stores GET /stores/bags(.:format) stores#bags
leisure_stores GET /stores/leisure(.:format) stores#leisure
quote_stores GET /stores/quote(.:format) stores#quote
stores GET /stores(.:format) stores#index
POST /stores(.:format) stores#create
new_store GET /stores/new(.:format) stores#new
edit_store GET /stores/:id/edit(.:format) stores#edit
store GET /stores/:id(.:format) stores#show
PATCH /stores/:id(.:format) stores#update
PUT /stores/:id(.:format) stores#update
DELETE /stores/:id(.:format) stores#destroy
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
root GET / stores#index
我遇到的问题是将:id引入'引用'视图。
我想看看我的路线; quote_stores GET /stores/quote/:id(.:format)store#quote或类似的东西。
可以:id只能通过CRUD传递吗?我以为我可以通过几乎任何地方传递实例变量,所以我在我的视图中将其写为视图的链接,其中:id信息传递给它。
<% @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_path(office) %></p>
<p class="addTo"><%= link_to 'Price Info', quote_stores_path(office) %></p>
</div>
<% end %>
我指的是&lt;%= link_to&#39; Price Info&#39;,quote_stores_path(office)%&gt;单击后,您将转到正确的视图,在URI路径中,它甚至列出了正确的:id,但它不会传递给视图:id&#39; s信息。
我的控制器代码如下: -
class StoresController < ApplicationController
add_breadcrumb 'home', :stores_path
def index
@products = Product.all
end
def show
@products = Product.find(params[:id])
if @products.nil?
redirect_to action: :index
end
add_breadcrumb 'Back', @products.section
end
def writing
@products = Product.where(:section => 'writing').paginate(:per_page => 5, :page => params[:page])
add_breadcrumb 'writing', writing_stores_path
end
def office
@products = Product.where(:section => 'office').paginate(:per_page => 5, :page => params[:page])
add_breadcrumb 'office', office_stores_path
end
def time
@products = Product.where(:section => 'time').paginate(:per_page => 5, :page => params[:page])
add_breadcrumb 'time', time_stores_path
end
def home
@products = Product.where(:section => 'home').paginate(:per_page => 5, :page => params[:page])
add_breadcrumb 'home', home_stores_path
end
def wellness
@products = Product.where(:section => 'wellness').paginate(:per_page => 5, :page => params[:page])
add_breadcrumb 'wellness', wellness_stores_path
end
def travel
@products = Product.where(:section => 'travel').paginate(:per_page => 5, :page => params[:page])
add_breadcrumb 'travel', travel_stores_path
end
def bags
@products = Product.where(:section => 'bags').paginate(:per_page => 5, :page => params[:page])
add_breadcrumb 'bags', bags_stores_path
end
def leisure
@products = Product.where(:section => 'leisure').paginate(:per_page => 5, :page => params[:page])
add_breadcrumb 'leisure', leisure_stores_path
end
def quote
@products = Product.find_by(params[:id])
if @products.nil?
redirect_to action: :index
end
end
end
除了我的代码不干,我在这里找不到什么?我不理解的是什么:id&#39; s?
答案 0 :(得分:0)
在你的config/routes.rb
中,你可能有这一行:
resources :stores
为stores
资源的标准CRUD操作创建路由。您可以为此资源定义其他操作,这些操作可以应用于集合(多个产品),也可以单独应用于成员(单个产品)。
请注意,为资源
products
而不是stores
命名可能更符合逻辑,因为它似乎正在处理Products
。
在您的情况下,您需要定义其他成员操作。由于它适用于单个产品,因此Rails将定义一个采用id
参数的路由:
resources :stores do
member do
get 'quote'
end
end
这将生成以下路线(rake routes
):
quote_store GET /stores/:id/quote(.:format) stores#quote
请注意,路线名为 quote_store ,是单数形式而非复数形式。
另见the Rails guides for more information about collection and member routes。