Question

I'm a rails beginner and I'm having a bit of trouble understanding how the render method works. I'm following along with this railscasts, which has to do with the ancestry plugin for rails. The part I'm having trouble understanding is the nested_forms helper method RyanB provides.

def nested_messages(messages)
  messages.map do |message, sub_messages|
  render(message) + content_tag(:div, nested_messages(sub_messages), :class =>     
  "nested_messages")
  end.join.html_safe
end

When render(message) is called, rails renders the _message.html.erb partial. How would I force it to render a different partial?

I've tried something like this:

def form_nodes(nodes)
 nodes.map do |node, sub_nodes|
   render(:partial => 'mypartial', :locals => node) + content_tag(:div, form_nodes(sub_nodes), :class => "nested_nodes")
 end.join.html_safe
end

But that results in an error:

undefined method `keys' for

I'm using Ancestry to create a survey form. Which allows the user to create a survey, and then take it. So I want rails to render two different types of views. One for creating the survey and one for taking the survey. So I'd like to create a separate partial for displaying my ancestry nodes, but I'm not sure how to force the render(message) method to render a different partial.

Any help would be appreciated.

Thanks

Was it helpful?

Solution

In case someone comes across this question. This is what I ended up doing to get it to work. I have no idea if this would be considered the best solution or not, but it worked for me. Feel free to correct my code if I did something stupid.

def form_nodes(nodes, answers)
     nodes.map do |node, sub_nodes|
       render(:partial => 'folder/mypartial', :locals => { :node => node, :answers => answers, :f => builder}) + content_tag(:div, form_nodes(sub_nodes,answers), :class => "nested_nodes")
     end.join.html_safe
 end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top