I have this function in module MessagesHelper:
def message_block( message, ticket, comment )
content_tag( :div, :class => "message", :id => "message-" + message.id.to_s ) do
content_tag( :p, message.body, :class => "message-body", :id => "message-body-" + message.id.to_s ) +
content_tag( :div, content_tag( :a, "add comment", :id => "comment-link-" + ticket.id.to_s, :class => "comment-link" ), :id => "comment-" + ticket.id.to_s, :class => "comment" ) +
form_for( comment, :html => { :class => "add-comment", :id => "add-comment-" + ticket.id.to_s } ) do |f|
f.label :body, "add comment"
f.text_area :body
f.submit "add comment"
end
end
end
To get this expected outcome:
<div class="message">
<p></p>
<div class="comment">
</div>
<form class="add-comment">
</form>
</div>
In my partial view, _messages.html.erb:
<% if !@message.nil? %>
<% @message.each do |m| %>
<%= message_block( m, @ticket, @comment ) %>
<% end %>
The text_area does not even load in the DOM and I can only see the add comment button. This is all in the TicketController (not MessageController or CommentController).
Any help with my problem would be great. Thank you.
You’ve got a quite hard to read and maintain helper. I would suggest you to consider usage of partial for it.
Here’s the example. Let’s say you want to render a lot of forms and you consider to create a helper which accepts some header, object for form and list of fields to be included in the form. Here the solution via partials.
Create in your
views/shared/_render_some_stuff.erb(note the underscore at the beginning):And “inject” it in any view this way:
(Note: here no underscore at the beginning. You just need it to distinguish your partial in a directory, but not here)
I guess it beats your approach?