0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】_[部分テンプレート名].html.erbについて

Last updated at Posted at 2025-04-30

記事概要

Ruby on Railsの部分テンプレートについて、まとめる

前提

  • Ruby on Railsでアプリケーションを作成している

基本情報

ファイルパス

app/views/[コントローラー名]/_部分テンプレート名.html.erb
app/views/shared/_部分テンプレート名.html.erb

記載方法

[アクション名].html.erbを、参照

まとめ

form_withメソッド

  1. ビューファイルのform_withメソッドを確認し、切り取る
    <h3>投稿する</h3>
    <%= form_with(model: @tweet, local: true) do |f| %>
      <!-- 処理 -->
    <% end %>
    
  2. ビューファイルから切り取ったコードを、部分テンプレート_form.html.erbに貼り付ける
    <%= form_with(model: @tweet, local: true) do |f| %>
      <!-- 処理 -->
    <% end %>
    
  3. modelオプションの変数から、@を削除する
    <%= form_with(model: tweet, local: true) do |f| %>
      <!-- 処理 -->
    <% end %>
    
  4. ビューファイルにrenderメソッドを追加する
    <h3>投稿する</h3>
    <%= render partial: "form", locals: { tweet: @tweet } %>
    
    • 部分テンプレートだと、model: tweetになっている
    • locals: { tweet: @tweet }と記述することで、@tweet → tweetと読み替えることができる

Ruby on Railsまとめ

ビュー

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?