Forum: Ruby on Rails Using same partial with diff ajax requests.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F677fa685a2cfe8aff31f161062db3d3?d=identicon&s=25 David (Guest)
on 2009-02-11 06:06
(Received via mailing list)
Im wondering what the best design implementation with DRY in mind in
the following situation.  I have a number of situations where I am
rendering the same partial with different ajax requests.  For example,
I have an inbox table and I have to link_to_remotes.  One links to
viewing all messages and another links to show a single message.  Im
wondering, does it make sense to use the same partial and create a
variable in the controller which could be used in an if else statement
within the view.  like:

def show
@type = "show"
page.replace_html "inbox_message", :partial => 'message', :locals =>
{:type => @type}
end

def index
@type = "show_all"
page.replace_html "inbox_message", :partial => 'message', :locals =>
{:type => @type}
end

then in the _message partial i could have somehting like:
if @type == "show"
  only show one message
else
  show all messages
end

Is there a better way to do this?  Would just creating two different
partials be any better?  What about any performance issues?

Thanks, Dave
8da92d4ed91aa12535f6d870fa76b25e?d=identicon&s=25 Aaron Baldwin (Guest)
on 2009-02-11 09:36
(Received via mailing list)
I think 2 partials would work nicely:

# _messages.html.erb
<h1>Listing Messages</h1>
<ul>
  <%= render :partial => 'message', :collection => @messages %>
</ul>

# _message.html.erb
<li><%= message.title %> - <%= message.body %></li>

Aaron
This topic is locked and can not be replied to.