Can''t work this out: In list.rhtml: <% for employee in @employees -%> <%= render(:partial => "list_form", :object => employee) %> <% end -%> In _list_form.rhtml (partial): <%= employee.id %> In controller: @employees = Employee.find(:all) I get the following error: undefined local variable or method `employee'' I everything is correct as far as I can see, just no variables are getting through? -- Posted via http://www.ruby-forum.com/.
James Whittaker wrote:> Can''t work this out: > > In list.rhtml: > <% for employee in @employees -%> > > <%= render(:partial => "list_form", :object => employee) %> > > <% end -%> > > In _list_form.rhtml (partial): > <%= employee.id %> > > In controller: > @employees = Employee.find(:all) > > I get the following error: > undefined local variable or method `employee'' > > I everything is correct as far as I can see, just no variables are > getting through?>From Rails API Docs:Rendering partials Partial rendering is most commonly used together with Ajax calls that only update one or a few elements on a page without reloading. Rendering of partials from the controller makes it possible to use the same partial template in both the full-page rendering (by calling it from within the template) and when sub-page updates happen (from the controller action responding to Ajax calls). By default, the current layout is not used. # Renders a collection of the same partial by making each element of @wins available through # the local variable "win" as it builds the complete response render :partial => "win", :collection => @wins E.g From my app: list.rhtml <% if !@contacts.empty? %> <%= render :partial => ''contacts'', :collection => @contacts, :locals => { :hidden => false } %> <% end %> _contacts.html <%= contact.name %> Hope this helps /Joseph -- Posted via http://www.ruby-forum.com/.
I believe that when you send a variable to a partial with the :object argument, it takes on the name of the partial. In your example, that would mean the variable would be called "list_form". If all else fails, you can use the :locals argument, like so: render(:partial => "list_form", :locals => { :employee => employee }) Jeff James Whittaker wrote:> Can''t work this out: > > In list.rhtml: > <% for employee in @employees -%> > > <%= render(:partial => "list_form", :object => employee) %> > > <% end -%> > > In _list_form.rhtml (partial): > <%= employee.id %> > > In controller: > @employees = Employee.find(:all) > > I get the following error: > undefined local variable or method `employee'' > > I everything is correct as far as I can see, just no variables are > getting through?-- Posted via http://www.ruby-forum.com/.
Jeff Coleman wrote:> > I believe that when you send a variable to a partial with the :object > argument, it takes on the name of the partial. > > In your example, that would mean the variable would be called > "list_form". > > If all else fails, you can use the :locals argument, like so: > > render(:partial => "list_form", :locals => { :employee => employee }) > > Jeff > > James Whittaker wrote: >> Can''t work this out: >> >> In list.rhtml: >> <% for employee in @employees -%> >> >> <%= render(:partial => "list_form", :object => employee) %> >> >> <% end -%> >> >> In _list_form.rhtml (partial): >> <%= employee.id %> >> >> In controller: >> @employees = Employee.find(:all) >> >> I get the following error: >> undefined local variable or method `employee'' >> >> I everything is correct as far as I can see, just no variables are >> getting through?Yes I have just realised that it takes the name of the partial as the local variable name. I have ran into another issue. I am using link_to_remote to update a form via ajax. However I need it to replace the div with a partial. However when pressed it jumps me to a blank screen? no error or anything. -- Posted via http://www.ruby-forum.com/.