[rails] How to display parent information in child view in nested relationships

Thing you want to do

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

procedure

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

1. Describe the relationship between parent items> child orders in routes.rb

resources :items do
    resources :orders, only: [:create, :index]

Now you can specify the relationship of (parent) item> (child) order. Can be nested

2. Write @item = Item.find (params [item_id]) in the orders controller

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

3. Write @item in the view file of order # index

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.

Summary

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

[rails] How to display parent information in child view in nested relationships
[Rails] How to display information stored in the database in view
[Rails] How to display an image in the view
[rails] How to display db information
How to set the display time to Japan time in Rails
[Rails] How to load JavaScript in a specific view
[Ruby On Rails] De-root_path! Redirect notation from a nested "child" view to a nested "parent": show
How to display a graph in Ruby on Rails (LazyHighChart)
How to introduce jQuery in Rails 6
How to install Swiper in Rails
How to write the view when Vue is introduced in Rails?
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
[Rails] How to display error messages individually
How to use MySQL in Rails tutorial
[rails] How to configure routing in resources
How to implement ranking functionality in Rails
[Rails] How to use video_tag to display videos
How to use credentials.yml.enc introduced in Rails 5.2
How to display error messages in Japanese
[Rails] How to get the user information currently logged in with devise
How to display the text entered in text_area in Rails with line breaks
[Ruby on Rails] How to display error messages
How to display a web page in Java
[Rails] How to use select boxes in Ransack
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
[Japanese localization] i18n rails Easy Japanese localization view display only
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
[Rails] How to convert UC time display to Japanese time display
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
How to write Rails
[Rails, JS] How to implement asynchronous display of comments
How to get information about associated tables in many-to-many tables
[Ruby On Rails] How to reset DB in Heroku
How to display a browser preview in VS Code
[How to insert a video in haml with Rails]
How to uninstall Rails
How to write a date comparison search in Rails
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to edit and customize devise view and controller
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
[Ruby On Rails] How to search and save the data of the parent table from the child table
Implementation policy to dynamically save and display Timezone in Rails
[Rails] How to define macros in Rspec and standardize processing
How to deploy jQuery in your Rails app using Webpacker
How to add the same Indexes in a nested array
[Rails] How to display the list of posts by category