"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
”
end
end
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:
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 C. [email protected]