On 4/28/06, Damian leGassick [email protected] wrote:
<%= SidebarItem('status') %>
##linked to a helper
def SidebarItem(name)
link_to_remote “#{name}”,
:url=> {:action => name, :id => @work.id, :layout => false }
end
I would take out this helper method altogether - it’s specifically
tied to the ivar @work, so you can’t really use it in any other
general sense. You could of course change it to:
def SidebarItem(name,ivar)
link_to_remote “#{name}”, :url => {:action => name, :id =
ivar.id, :layout => false }
end
and then call it using:
SidebarItem(‘catalogue’,@work)
so that it would be applicable to more cases, but then you may as well
just use the regular link_to_remote function. I only really use
helpers where they replace a large chunk of code (not just a one
liner), or I’ll be calling them over and over again from different
parts of the application (which of course you could do with the second
form I presented here, since it’s more general - but like I said, for
a simple one liner like link_to_remote, you may as well keep it
there).
In any case, why not do something like this:
myview.rhtml:
<%= link_to_remote 'catalogue', :url => {:action => my_dispatcher,
:id = @work.id, :arg => 'catalogue', :layout => false } %>
<%= link_to_remote 'provenance', :url => {:action =>
my_dispatcher, :id = @work.id, :arg => 'provenance', :layout => false
} %>
<%= link_to_remote 'status', :url => {:action => my_dispatcher,
:id = @work.id, :arg => 'status', :layout => false } %>
my_controller.rb:
def my_dispatcher
return unless @updated
partial_name = params[:arg]
#use inline rjs
render :update do |page|
page.replace_html ('EditFields', :partial => partial_name)
end
end
You’ll still need three different partial templates,
‘_catalogue.rhtml’, ‘_provenance.rhtml’ and ‘_status.rhtml’, but at
least this will allow you to remove the three separate .rjs template
files. If the three partials contain essentially the exact same
layout, you could also condense them into a single partial file, and
determine which values to change in the partial by passing local
variables. ie, if your partials only do something like:
_catalogue.rhtml:
Enter catalogue name: <%= text_field (‘model’, ‘catalogue_name’, …) %>
then you could change the render method in my_displatcher to:
def my_dispatcher
return unless @updated
item_name = params[:arg]
#use inline rjs
render :update do |page|
page.replace_html ('EditFields', :partial => 'standard',
:locals => { :item => item_name })
end
end
and then remove _catalogue.rhtml, _provenance.rhtml, _status.rhtml and
replace it with _standard.rhtml:
Enter <%= item_name %> name: <%= text_field (‘model’, item_name, …) %>
or something to that effect.
Mike