一個id如果我向您展示了我的路線開始了這個問題: -傳遞軌道4,5
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
我有越來越的問題:身份證進入「報價」視圖。
我想在我的路線中看到; quote_stores GET /stores/quote/:id(.:format)store#quote或者類似的東西。
可以:id只能通過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 %>
我這裏指的是<%=的link_to「價格信息」,quote_stores_path(辦公室)%>這在點擊帶您到正確的觀點,並在URI路徑甚至列出了正確的不會傳遞到: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
所以除了我的代碼不是乾的,什麼是我在這裏失去了嗎?我不明白什麼:id的?
五位數,我沒有聽說過這個成員動作,感謝你的資源鏈接和解決問題的非常明確的答案,非常感謝。 – 2014-09-20 22:17:10
不客氣! – fivedigit 2014-09-21 05:24:21