If I have several controller actions which have this same line of code (or more), how can I DRY up the line without running into complaints about redirecting or rendering only once? If I put the line in another method in my controller, won''t it think I''m redirecting? I want to dry up lines like this: @listing = Listing.find(params[:id]) def customerview @listing = Listing.find(params[:id]) render :action => ''edit'' end def view @listing = Listing.find(params[:id]) render :action => ''edit'' end def edit @listing = Listing.find(params[:id]) render :action => ''edit'' end Thanks, Eric -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060429/6e84df91/attachment.html
You can add non-routable methods into controllers by making the method private: def customerview find_listing # .. rest of code end def view find_listing # .. rest of code end private def edit_listing @listing = Listing.find(params[:id]) render :action => ''edit'' end Steve Eric Beland wrote:> If I have several controller actions which have this same line of code > (or more), how can I DRY up the line without running into complaints > about redirecting or rendering only once? If I put the line in another > method in my controller, won''t it think I''m redirecting? > > I want to dry up lines like this: @listing = Listing.find(params[:id]) > > def customerview > @listing = Listing.find(params[:id]) > render :action => ''edit'' > end > > def view > @listing = Listing.find(params[:id]) > render :action => ''edit'' > end > > def edit > @listing = Listing.find(params[:id]) > render :action => ''edit'' > end > > Thanks, > > Eric > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
This isn''t quite right actually. Should be: def customerview # .. any other code find_listing end def view # .. any other code find_listing end private def edit_listing @listing = Listing.find(params[:id]) render :action => ''edit'' end That''s if you''re rendering the output in the edit_listing method. Steve Stephen Bartholomew wrote:> You can add non-routable methods into controllers by making the method > private: > > def customerview > find_listing > # .. rest of code > end > > def view > find_listing > # .. rest of code > end > > private > def edit_listing > @listing = Listing.find(params[:id]) > render :action => ''edit'' > end > > Steve > > Eric Beland wrote: >> If I have several controller actions which have this same line of code >> (or more), how can I DRY up the line without running into complaints >> about redirecting or rendering only once? If I put the line in >> another method in my controller, won''t it think I''m redirecting? >> >> I want to dry up lines like this: @listing = Listing.find(params[:id]) >> >> def customerview >> @listing = Listing.find(params[:id]) >> render :action => ''edit'' >> end >> >> def view >> @listing = Listing.find(params[:id]) >> render :action => ''edit'' >> end >> >> def edit >> @listing = Listing.find(params[:id]) >> render :action => ''edit'' >> end >> >> Thanks, >> >> Eric >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Eric Beland wrote:> If I have several controller actions which have this same line of code > (or more), how can I DRY up the line without running into complaints > about redirecting or rendering only once? If I put the line in another > method in my controller, won''t it think I''m redirecting? > > I want to dry up lines like this: @listing = Listing.find(params[:id])You could use a before_filter: class MyController < ApplicationController before_filter :find_listing def customerview render :action => ''edit'' end def view render :action => ''edit'' end def edit end private def find_listing @listing = Listing.find(params[:id]) end end -- Philip Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby