Rob Gabaree
2006-Aug-17 21:07 UTC
[Rails] What''s the best way to stop repeating this line in my controller?
Hi, I am trying to follow the "DRY" principle and came across a problem. I am working on a small ajax task manager, and right now in the index page which renders the list, I have (for example, in the Lists controller): def index @lists = List.find(:all) end Now within the controller there''s also a list_add action which is called via ajax.. def list_add if List.create(params[:list]) @lists = List.find(:all) render(:partial => "listform") end end I need to put the @lists = List.find(:all) in it again so it gets the new updated list to render via AJAX like I did in the index action, and I have to do this for every action I need... Is there a way to do this so I can stop repeating myself? Or am I outta luck? Thanks.
zer0halo
2006-Aug-17 21:14 UTC
[Rails] What''s the best way to stop repeating this line in my controller?
use a :before_filter at top of your controller: before_filter :get_list then add: private def get_list @lists = List.find(:all) end now get_list will be called before index, list_add, or any other controller methods. If you just want it for those two change the top line to: before_filter :get_list, :only => [:index, :list_add] you can also use :except => [ ... ] On 8/17/06, Rob Gabaree <lists@rawb.net> wrote:> > Hi, > > I am trying to follow the "DRY" principle and came across a problem. > I am working on a small ajax task manager, and right now in the index > page which renders the list, I have (for example, in the Lists > controller): > > def index > @lists = List.find(:all) > end > > Now within the controller there''s also a list_add action which is > called via ajax.. > > def list_add > if List.create(params[:list]) > @lists = List.find(:all) > render(:partial => "listform") > end > end > > I need to put the @lists = List.find(:all) in it again so it gets the > new updated list to render via AJAX like I did in the index action, > and I have to do this for every action I need... > > Is there a way to do this so I can stop repeating myself? Or am I > outta luck? > > Thanks. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- "Impossible is nothing." -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060817/be86f5fa/attachment.html
B Gates
2006-Aug-17 23:36 UTC
[Rails] What''s the best way to stop repeating this line in my controller?
I don''t think the before_filter will work if you need to have the action reflect the List.create step. What about def list_add if List.create(params[:list]) index render(:partial => "listform") end ? Cheers, B Gates I am trying to follow the "DRY" principle and came across a problem. I am working on a small ajax task manager, and right now in the index page which renders the list, I have (for example, in the Lists controller): def index @lists = List.find(:all) end Now within the controller there''s also a list_add action which is called via ajax.. def list_add if List.create(params[:list]) @lists = List.find(:all) render(:partial => "listform") end __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
zer0halo
2006-Aug-18 00:00 UTC
[Rails] What''s the best way to stop repeating this line in my controller?
On 8/17/06, B Gates <bmathg@yahoo.com> wrote:> > I don''t think the before_filter will work if you need > to have the action reflect the List.create step. What > about > def list_add > if List.create(params[:list]) > index > render(:partial => "listform") > end > > >You''re right; I hadn''t looked that carefully at your list_add function. However, if what you''re trying to do is use ajax to add an item to a task list on a page, then you shouldn''t need to repopulate the your @lists collection. Instead your listform partial should contain the view for a single record. The index view would iterate through record set and show the partial for each one. So what you should do is: def index @lists = List.find(:all) end def list_add if List.create(params[:list]) render(:partial => "listform") end end Then in your index view, you have something like this: <% for @list in @lists %> <%= render(:partial => ''listform'') %> <% end %> In _listform.rthml you then have the code that displays a single item (which you refer to as @list; ie, @list.title, etc.) In fact, you can take it a step further and use RJS. Get rid of the render line in your list_add method. In the same folder as your index.rhtml, create a file called list_add.rjs. Your list_add method will automatically look for such a file and execute it if found. Add the following line to your list_add.rjs file: page.insert_html :bottom, ''id-of-your-task-div-or-table'', :partial => ''listform'' This will add a new instance of your listform partial at the end of your list. -- "Impossible is nothing." -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060818/8918438e/attachment.html
Possibly Parallel Threads
- How to update values in database in this form?
- Trigger ajax form submission with drop_receiving_element
- names of select elements inside a fields_for block not generated as expected
- del.icio.us api - repeating in my layout??? HELP!
- Save and delete buttons: POST vs. proximity dilemma