Quick question about helpers

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:

Last 5 posts

<% @recent_posts.each do |r| %>

<%= link_to r.post_title, {:action => "read", :id => r}, :class => "jump" %>

<% end %>

I have had success inserting straight HTML into my views by doing
something like this:

def insert_some_html
html = ‘stuff</html tags>’
return html

…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?


def insert_some_html_with_stuff(stuff)
“#{stuff}</html tags>”

should do at least part of the trick?

"I have had success inserting straight HTML into my views by doing
something like this:

def insert_some_html
html = ‘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)

This is a test!

Pure HTML code should be inserted here

If you want to use Ruby code use <%= 'this' %> form.

<% for client in @customers do %> <%= h client.name %> <% end %>

Notice that '<% %>' is the container for Ruby code. '<%=' returns the code inside the container. '<%' does not return code, used for blocks.

To insert HTML in your helpers you can use something like

module MyHelper
def show_last_5_posts

This will return
all this code


Then in the view you would use:

Last five posts:

<%= 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):

Last five posts:

<% for post in posts %> <%= h post.title %>
<%= h post.text %>
<% end %>

In the view (ie home.rhtml) you would have

Hi all!

<%= 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:

Will help you a lot. Sure did help me!

On Dec 11, 1:04 am, Sean C. [email protected]

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs