I'm working on a project right now, and one of the models is
Company.... simple regular company.
Now I've created some AJAX type searches in my project that rely on
searching on companies, which I use the INDEX action of
company_controller and pass a parameter which returns my companies,
using an RJS file I do my AJAX... this is what index.js.rjs looks like
for my company_controller.
Simple... now where I'm confusing myself is that I use the index
action to generate a list of companies several times... but the way I
have ti setup, I'm foreced to render search_results each time... I'm
curious what you guys think the best way to get around this is...
putting conditions in the RJS?
if params[:flag] == red
page.replace_html 'results', :partial => '/
search_results_no_widgets'
else
page.replace_html 'results', :partial => '/
search_lists_with_widgets'
end
or is the better solution to create a "format" for each one and add
another format to the index action...
respond_to do |format|
format.html # index.html.erb
format.js #index.js.erb ( widgets )
format.js2 #index.js2.rjs ( no widgets )
format.xml { render :xml => @companies }
end
Or is the better solution something else entirely?
I've created some AJAX type searches in my project that rely
on searching on companies, which I use the INDEX action of
company_controller and pass a parameter
While 'index' is an OK choice here, given the default action in Rails, you
might want to consider using the 'list' method directly instead. 'index' is
the default action for any url. What action that actually is a design
choice.
Simple... now where I'm confusing myself is that I use the index
action to generate a list of companies several times... but the way I
have ti setup, I'm foreced to render search_results each time... I'm
curious what you guys think the best way to get around this is...
putting conditions in the RJS?
if params[:flag] == red
page.replace_html 'results', :partial => '/
search_results_no_widgets'
else
page.replace_html 'results', :partial => '/
search_lists_with_widgets'
end
I would definitely recommend this to the other solution you're considering.
From a 'personal preference' perspective, i'd assign params[:flag] to an
instance variable in the controller. Better decoupling, IMHO.
the LIST command isn't part of the 7 REST actions... so using list, is
un-restful!
I'm still not convinced that the if statment in the RJS file is the
way to go.... for fun I made a new RJS file, called it index.js2.rjs
and made a new MIME type in environment.rb and it seems to work
well... I've got to maintain two RJS files, but to me it makes sense
because they're rendering two different views using two different
partials... the only thing that is the same is the controller action
that fetches the company...
doing the If in the RJS kinda fells as dirty as putting an IF in the
view....
Regardless if I'm using "index" or "list" it still doesn't solve my
issue. If both calls were made to the list command, my question is
would I use a new format to get two different outputs ( views ) or is
the correct way to use if statements in the RJS files? I'm leaning
the way of the new format, just cuz it's easier, and cleaner... my
call askes for formatted_company_path(js2) and I get the index.js2.rjs
file... makes sense to me... I'm curious to hear from others on the
drawbacks of doing it this way... and on LIST being RESTful for that
matter..
I'll do some more reading, but it's been my understanding that it is
not.