1

I am trying to pass a variable to a partial so that it can be called within the partial This is how i am rendering the partial

 = render :partial => "layouts/reveal_delete", :resource => @schedule

and this is how i am calling the variable within the partial, though it doesnt appear to be working

#RevealDelete.reveal-modal
%a.close-reveal-modal ×
  %h3= "Delete #{@resource}"
  %p Are you sure you want to delete this?
  =link_to "Delete #{@resource}", @resource, :method => :delete, :class => "button close-reveal-modal"
  %a.button.alert.close-reveal-modal Cancel
Boss Nass
  • 3,384
  • 9
  • 48
  • 90

1 Answers1

3

Call it this way:

= render :partial => "layouts/reveal_delete", :locals => { :resource => @schedule }

And then within the partial, you can use it by referring to resource (no @), like this:

%h3= "Delete #{resource}"

Typically, though, you would name your local variable schedule to match the instance variable, so:

= render :partial => "layouts/reveal_delete", :locals => { :schedule => @schedule }

And then you can refer to schedule in your partial.

Also if you want, you can drop the :partial and :locals option keys, and use this shorter syntax:

= render "layouts/reveal_delete", :schedule => @schedule

Here, Rails assumes that if you pass in a string as your first argument, then the first argument is your partial name and the remainder is interpreted as local variable assignments. Here's an answer discussing this syntax:

Also more info in the docs.

Community
  • 1
  • 1
Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82
  • perfect, though i was told :locals was depreciated in rails 3 – Boss Nass Feb 03 '13 at 05:16
  • 2
    I see it in the docs, so it should still be ok to use. But you can also drop the `:partial =>` key and `:locals =>`, which makes it shorter: `= render "layouts/reveal_delete", :schedule => @schedule`. – Chris Salzberg Feb 03 '13 at 05:18
  • how would i use a link_to similar to this `%li= link_to "javascript:void(0);", :data => {"reveal-id" => "RevealDelete"}, :item => full_name(user), :resource => user` and have the variables passed to the partial. as i only want to render the partial once and be able to exchange variables – Boss Nass Feb 03 '13 at 05:41
  • Sorry I don't understand what you mean. – Chris Salzberg Feb 03 '13 at 05:47
  • say i had multiple users within a table, and option to delete them, id only want to render the partial for deleting once, and then pass their variables through – Boss Nass Feb 03 '13 at 05:57