Does anybody know where I can get a quick and dirty rundown on the rules for making helpers? I have some code that I want to extract from my view and turn into a helper method, but since it''s a mix of ruby and html, I''m stymied. Here it is, for reference: <h2 class="sidebar_header">Last 5 posts</h2> <% @recent_posts.each do |r| %> <h3><%= link_to r.post_title, {:action => "read", :id => r}, :class => "jump" %></h3> <% end %> <br /><br /> I have had success inserting straight HTML into my views by doing something like this: def insert_some_html html = ''<html tags>stuff</html tags>'' return html end ...but this scheme doesn''t work when there is code included in with the html, as above. I''ve been all over google, but I still can''t figure this out. It seems to be one of those things that the people who write tutorials and documentation figure is so basic that everybody should know it by now. Can anybody help? Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
def insert_some_html_with_stuff(stuff) "<html tags>#{stuff}</html tags>" end should do at least part of the trick? -- 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 -~----------~----~----~----~------~----~------~--~---
felipekk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Dec-11 09:44 UTC
Re: Quick question about helpers
"I have had success inserting straight HTML into my views by doing something like this: def insert_some_html html = ''<html tags>stuff</html tags>'' return html end " I think you wanted to say "inserting straight HTML into my helpers". In case you really meant views, they should look like: /views/test/test.rhtml (or test.erb.html if you are using Rails 2.x) <h3>This is a test!</h3> <p>Pure HTML code should be inserted here</p> <p>If you want to use Ruby code use <%= ''this'' %> form.</p> <% for client in @customers do %> <%= h client.name %> <% end %> <p>Notice that ''<% %>'' is the container for Ruby code. ''<%='' returns the code inside the container. ''<%'' does not return code, used for blocks.</p> To insert HTML in your helpers you can use something like module MyHelper def show_last_5_posts "<p>This will return all this code</p>" end end Then in the view you would use: <p>Last five posts:</p> <%= show_last_5_posts %> I don''t like this approach. It inserts HTML code inside Ruby code, which is ugly. I use helpers to format links, strings, etc, like, for example, link_to_if_authorized. If you have this "static" HTML code that shows the last 5 posts, you can create a partial. A partial is just like a view, it is named _partial.rhtml (for example _test.rhtml for a test partial) and you can render it trough <%= render :partial => ''test'' %>. So, a good approach for you would be to have a partial named recent_posts (file name _recent_posts.rhtml): <p>Last five posts:</p> <% for post in posts %> <%= h post.title %><br /> <%= h post.text %><br /> <% end %> In the view (ie home.rhtml) you would have <p>Hi all!</p> <%= render :partial => ''recent_posts'', :locals => { :posts => @posts } %> The :locals => { :posts => @posts } thingy in the render call above tells that the local variable ''posts'' in the partial receives the @posts variable (which should exist in the view). You should read this article: http://www.railsdev.ws/blog/wp-content/uploads/2007/10/modular_page_assembly_in_rails.pdf Will help you a lot. Sure did help me! On Dec 11, 1:04 am, Sean Cahoon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Does anybody know where I can get a quick and dirty rundown on the rules > for making helpers? I have some code that I want to extract from my view > and turn into a helper method, but since it''s a mix of ruby and html, > I''m stymied. > > Here it is, for reference: > > <h2 class="sidebar_header">Last 5 posts</h2> > <% @recent_posts.each do |r| %> > <h3><%= link_to r.post_title, {:action => "read", :id => r}, :class > => "jump" %></h3> > <% end %> > <br /><br /> > > I have had success inserting straight HTML into my views by doing > something like this: > > def insert_some_html > html = ''<html tags>stuff</html tags>'' > return html > end > > ...but this scheme doesn''t work when there is code included in with the > html, as above. I''ve been all over google, but I still can''t figure this > out. It seems to be one of those things that the people who write > tutorials and documentation figure is so basic that everybody should > know it by now. Can anybody help? > > Thanks > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Of course! I''m so stupid! Now I *finally* get the difference between helpers and partials. Thanks. This should help me organize my projects much better. Thanks for your patience. -- 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 -~----------~----~----~----~------~----~------~--~---