Premise ・ Order information does not hold without Item information (Order has an item_id column (which item information does it point to?)) (Parent) item table> (child) order table.
Invoke the index action on the order controller (index.html.erb) How do you describe if you want to display the item information of (parent)?
order table item_id:1 user_id:3 The stuffed animal 4000 yen and the user_id are tied together ...
config/routes.Description of rb
Rails.application.routes.draw do
devise_for :users
root 'items#index'
resources :items do
resources :orders, only: [:create, :index]
end
end
resources :items do
resources :orders, only: [:create, :index]
Now you can specify the relationship of (parent) item> (child) order. Can be nested
def index
@item = Item.find(params[:item_id])
end
Put the item table information in the @item box. Which item table information to specify can be specified by [: item_id] Assign the item information selected by the user to @item
order controller index.html.Described in erb
<%= @item.item_name %>
Now extract the data from the (parent) item table (Child) It can be displayed in the view of index.html.erb of the order controller.
It's a confusing summary In short, if you want to extract the information of the parent table and display it in the child view If you use nesting to show the relationship and write params [: parent_id] in the child controller The content was that information could be extracted from the parent and displayed in the child's view.
It's my lack of power that I can't put together well ... It may be easier to understand if specific images and table information are attached.
Recommended Posts