2

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

4

1 に答える 1

0

誰かがこの質問に出くわした場合。これが私がそれを機能させるためにやったことです。これが最善の解決策と見なされるかどうかはわかりませんが、私にとってはうまくいきました。私がばかげたことをした場合は、遠慮なくコードを修正してください。

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
于 2013-02-03T18:54:49.280 に答える