5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

入力フォームとかで部分テンプレートをレンダリングするとき、render から変数を渡す方法

Last updated at Posted at 2016-02-18

例えば、こんなの

#new.html.erb
<% @page_title = "会員の新規登録" %>

<h1><%= @page_title %></h1>

<%= form_for @member do |form| %>
<% render "form", form: form %>
<p><%= form.submit %></p>
<% end %>

_form.html.erbをレンダリングするが、ブロックパラメータである変数formをレンダリング先に渡さないと、フォームビルドできない!!

そこで、renderメソッドの第2引数として、変数formをレンダリング先に渡す!!

<% render "form", form: form %>

ハッシュのインデックス部分(form:)が、レンダリング先で使えるようになる。
☆参考: render から変数を渡す方法
http://d.hatena.ne.jp/shio_chan/20120323/1332511842

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?