LoginSignup
36
25

More than 5 years have passed since last update.

部分テンプレートにlocals:を使って変数を渡す時は partial:もつけないとエラー

Posted at

部分テンプレートに変数を渡そうとして少しハマったので記録

まず初めはこんな感じ

<%= render "list_footer", username: @username %>

もう一つ変数を増やしたかったので @listnameを追加

<%= render "list_footer", locals: {username: @username, listname: @listname} %>

→「listname or username なんて変数ねーよ」と怒られる

解決策

localsなどのキーを指定するなら全て指定、指定しないなら一つも指定してはいけないらしい。引数の解釈の問題かな?

つまり

<%= render "list_footer", {username: @username, listname: @listname} %>

<%= render partial: "list_footer", locals: {username: @username, listname: @listname} %>

のどちらかにしましょう。

36
25
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
36
25