LoginSignup
2
0

More than 1 year has passed since last update.

Passing Variable to Partial - undefined local variable or method error 2つの解決法

Last updated at Posted at 2020-05-23

1: render partial 'my_partial' と書いているか、render 'my_partial' と書いているか確認する

呼び出し方に違いがあります

<%= render 'my_partial', :locals => {:class_Name => "Science", :y => 36} %>
<% render partial: 'my_partial', :locals => {:class_Name => 'Science', :y => 36 } %>

2: 定義されているか確認する

# Rails4以降
<% if defined?(local_var) do %>
# Rails3
<% if local_assigns[:local_var].present? do %>

自分の場合、partialに書いた変数を使わない場合があり、 if defined?(local_var)を書いたら解決した。defined?で確認して、local_varをそのまま使わないようにした。

参考

自分はqiitaの記事のおかげで解決しました
https://qiita.com/shinichinomura/items/1921027fc28279ce54e0
https://stackoverflow.com/questions/16229470/passing-variable-to-partial-undefined-local-variable-or-method-error/21651848#21651848

2
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
2
0