How do you generate html outside of a view? I''m trying to use link_to to generate html for a link. I''ve included ActionView, ActionView::Helpers, ActionView::Helpers::UrlHelper, and ERB::Util, but the system says I''m calling url_for from a nil object. I checked my user code and added code to make sure each object exists (is not null) before trying to use it. I can''t figure out what I might have missed, and the error seems to be occurring deep inside the libraries. So maybe something else isn''t getting included? Here''s the stack trace in case that helps: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.url_for RAILS_ROOT: /usr/home/dan/progg/rails/articles/config/.. Application Trace | Framework Trace | Full Trace /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/ helpers/url_helper.rb:27:in `send'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/ helpers/url_helper.rb:27:in `url_for'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/ helpers/url_helper.rb:75:in `link_to'' /usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb: 25:in `node_html'' /usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb: 15:in `children_html'' /usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb: 14:in `each'' /usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb: 14:in `children_html'' /usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb: 40:in `tree_html'' /usr/home/dan/progg/rails/articles/app/controllers/ category_controller.rb:17:in `tree'' /usr/local/bin/mongrel_rails:19:in `load'' /usr/local/bin/mongrel_rails:19 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
danb wrote:> How do you generate html outside of a view? I''m trying to use link_to > to generate html for a link.i am not 100% sure what you are trying to do.... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i don''t get the details, why use this outside a view? and if necessary, where exactly did you place the rb file containing your code? otherwise the stack trace looks not that strange. if all objects exist and are not nil, my guess would be, that your routing has a bug. does link_to with the same attributes work in a view? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
danb wrote:> How do you generate html outside of a view? > I''m trying to use link_to to generate html for a link.gemblon (t.b.) wrote:> i am not 100% sure what you are trying to do....Thorsten Mueller wrote:> i don''t get the details, why use this outside a view? and if necessary, > where exactly did you place the rb file containing your code?in helpers/category:> module CategoryHelper > class CategoryHtml < Category> otherwise the stack trace looks not that strange. if all objects exist > and are not nil, my guess would be, that your routing has a bug. > does link_to with the same attributes work in a view?Yes, I copied the code straight out of a view that was working for a flat list. The db contains a virtual tree structure, sort of like a file system. Each row (node) a has a parent_id column that points to the row that the current row is a branch of. The parent_id of the root node/row is nil/ null. Now I want to view the tree by recursively calling a method that returns a piece of html for the row (branch in the tree) passed to it as an argument. All the little pieces of html get concatenated and pasted into a view as a single string of pure html. But views don''t take arguments, do they? A view would work if it could take an argument, but it doesn''t look like they can. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > But views don''t take arguments, do they? A view would work if > it could take an argument, but it doesn''t look like they can.in erb tags? whatever you define in a controller with an @ can be used in the view so in the controller: @foo = 5 and in the view: <%= @foo %> would output 5 and of course you can use code: <ul> <% @mylist.each do |list| %> <li><%= list.name %></li> <% end %> </ul> woul throw out a ul with names out of the list. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > But views don''t take arguments, do they? A view would work if > > it could take an argument, but it doesn''t look like they can.On Dec 3, 1:13 pm, Thorsten Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> in erb tags? whatever you define in a controller with an @ > can be used in the viewRight, but what I''m talking about here is a partial that has multiple copies rendered together on a page, and while the html is being generated, there''s a stack of calls to the partial that have been entered, but not yet exited. The stack contains the rows in the db corresponding to a chain of nodes in the tree, So there would be multiple values of @category or whatever that have to be remembered while rendering the page, and the number and structure of the rows is determined at runtime. It''s a lot harder without a method or something that can take its own local argument. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
partials can take arguments with :locals like <%= render :partial => "my_partial", :locals => {:category => @category} %> you can make that as complex as you want by handing over arrays or hashes and of course, the partial could render itself recursively, since it is a kind of method anyway -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 3, 1:52 pm, Thorsten Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> partials can take arguments with :locals like > <%= render :partial => "my_partial", > :locals => {:category => @category} %>That''s it! Thorsten, you''re a genius! My gosh, I was starting to lose hope. Anyway, the code''s working now, so my faith in Rails is restored. Thanks Thorsten, I appreciate the help. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---