0

How can I pass a variable to a partial using this code? :

<%= render @performance_indicator.improvement_actions.order("created_at DESC") %>

I want to pass "id=1" and then in _improvement_action, use that variable like:

<%= id %>

EDIT:

This is my improvement_action partial:

https://gist.github.com/luisamaro0/6597084f2de1dc33cde7c014ea9f23a5

terrorista
  • 227
  • 1
  • 15
  • 1
    Possible duplicate of [Rails 3, passing local variable to partial](http://stackoverflow.com/questions/6279651/rails-3-passing-local-variable-to-partial) –  Mar 31 '16 at 02:16

3 Answers3

3

You can pass a local variable like so:

render "a_partial", :a_local_variable => whatever, :another_variable => another

See this question for more details: Rails 3, passing local variable to partial

Community
  • 1
  • 1
  • i cant pass a created local variable ? – terrorista Mar 31 '16 at 02:13
  • Yes, you can, my mistake for misunderstanding your question. I've updated my answer and linked you to a similar question. –  Mar 31 '16 at 02:16
  • I saw that question before and tried like that: `<%= render @performance_indicator.improvement_actions, :a_local_variable => 'whatever' %>` but dont work, when calling the variable in the partial – terrorista Mar 31 '16 at 02:25
  • It works for me, are attempting to access the local variable as a symbol? (You shouldn't be) –  Mar 31 '16 at 02:27
  • im trying to access like this: `<%= a_local_variable %>` – terrorista Mar 31 '16 at 02:29
  • If you try <%= local_variable.nil? %> what shows up in your page? –  Mar 31 '16 at 02:31
  • undefined local variable or method `local_variable' for #<# – terrorista Mar 31 '16 at 02:32
  • The code I posted was an example, when copying and pasting you need to replace "local_variable" with the name of your real local variable. –  Mar 31 '16 at 02:33
  • i rendered like this: `<%= render @performance_indicator.improvement_actions, :local_variable => '1' %>` and called the variable like this: `<%= local_variable %>` – terrorista Mar 31 '16 at 02:35
1

you can pass a variable like this

<%= render partial: 'partial_name', locals: {id: '1'} %>
Narasimha Reddy - Geeker
  • 3,510
  • 2
  • 18
  • 23
0

Try this syntax out:

<%= render @performance_indicator.improvement_actions, locals: { local_variable: 1 %>
# then <%= local_variable %>
Ho Man
  • 2,308
  • 11
  • 16