Ruby on rails 4 在Rails 4中传递id
如果我首先向您展示我的路线:-Ruby on rails 4 在Rails 4中传递id,ruby-on-rails-4,Ruby On Rails 4,如果我首先向您展示我的路线:- 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(.:form
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'视图
我想看看我的路线;quote#stores GET/stores/quote/:id(:format)stores#quote或类似的东西
只能通过CRUD传递id吗??我想我几乎可以在任何地方传递实例变量,所以我在视图中编写了这个链接,作为指向视图的链接,其中传递了: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 %>
我指的是,单击它会将您带到正确的视图,在URI路径中它甚至列出了正确的:id,但是它不会传递到:id信息的视图中
我的控制器代码如下:-
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
class StoresControllerwriting').paginate(:per_page=>5,:page=>params[:page])
添加面包屑“写入”,写入存储路径
结束
国防部办公室
@products=Product.where(:section=>office').paginate(:per_page=>5,:page=>params[:page])
添加面包屑“办公室”,办公室存储路径
结束
def时间
@products=Product.where(:section=>time').paginate(:per_page=>5,:page=>params[:page])
添加面包屑“时间”,时间存储路径
结束
def主页
@products=Product.where(:section=>home').paginate(:per_page=>5,:page=>params[:page])
添加面包屑“主页”,主页存储路径
结束
def健康
@产品=产品。其中(:section=>wellness')。分页(:per_page=>5,:page=>params[:page])
添加面包屑“健康”,健康商店路径
结束
def旅行
@products=Product.where(:section=>travel').paginate(:per_page=>5,:page=>params[:page])
添加面包屑“旅行”,旅行存储路径
结束
def袋
@产品=产品。其中(:section=>bags')。分页(:per_page=>5,:page=>params[:page])
添加面包屑“bags”,bags\u stores\u路径
结束
def休闲酒店
@products=Product.where(:section=>leisure').paginate(:per_page=>5,:page=>params[:page])
添加面包屑“休闲”,休闲商店路径
结束
def报价
@products=Product.find_by(参数[:id])
如果@products.nil?
将_重定向到操作::索引
结束
结束
结束
所以除了我的代码不干之外,我还缺少什么?关于id,我还不了解什么?在您的
配置/routes.rb
中,您可能有以下行:
resources :stores
它为存储
资源的标准CRUD操作创建路由。您可以为此资源定义其他操作,这些操作可以应用于集合(多个产品)或单个成员(单个产品)
请注意,将资源命名为产品
可能比命名为商店
更合理,因为它似乎在处理产品
在您的情况下,您需要定义一个额外的成员操作。由于它适用于单个产品,Rails将定义一个采用id
参数的路由:
resources :stores do
member do
get 'quote'
end
end
这将生成以下路由(rake路由
):
请注意,该路由称为quote_store,是单数形式,而不是复数形式
另请参见。fivedigit,我没有听说过会员行动,非常感谢您提供的资源链接和解决问题的非常清晰的答案,非常感谢。
quote_store GET /stores/:id/quote(.:format) stores#quote