Hello everyone! I have generic views for CRUD operations For example I render(''shared/list'') from controller shared/list.rhtml ... <% for item in @list %> <tr class="<%= cycle ''list-line-odd'', ''list-line-even'' %>"> <%= render :partial => ''item'', :locals => { :item => item } %> <td><%= link_to ''Edit'', :action => :edit, :id => item %></td> <td><%= link_to ''Destroy'', { :action => :destroy, :id => item }, :confirm => "Delete: #{item.name} ?", :post => true %></td> </tr> <% end %> ... As you see I also need to render :partial => ''list'' on every iteration to add some custom information for a particullar controller. districts/_item.rhtml ... <td><%= item.name %></td> <td><%= item.region.name %></td> ... As you may already guess I''m getting ActionController::DoubleRenderError So is there better way of doing this, or maybe some other function method such as: render_and_not_raise_DoubleRenderError exist? :) Any ideas? Someone proposed to use render_to_string, but it does not work in views. Or creating an array with items in controller that will be used in template to populate the view. But its not MVC way of doing things. I mean common! It should be simple, as a method call! Thanks for help! -- Posted via http://www.ruby-forum.com/.
Its all working now! Sorry, my bad! It was hapening bacause I didn''t remove render method from my controller. This method was called first and then one from generic controller By the way is this http://dereksivers.com/rails-shared-controller.html a good way for DRYing the CRUD controllers or there are better ways? Thanks! -- Posted via http://www.ruby-forum.com/.
> By the way is this http://dereksivers.com/rails-shared-controller.html > a good way for DRYing the CRUD controllers or there are better ways? >That is very cool! I''m curious to know if any of the senior railers out there endorse that methodology! It looks very promising. Michael -- Posted via http://www.ruby-forum.com/.
On 8/3/06, Michael Modica <codeslush@yahoo.com> wrote:> > By the way is this http://dereksivers.com/rails-shared-controller.html > > a good way for DRYing the CRUD controllers or there are better ways? > That is very cool! I''m curious to know if any of the senior railers out > there endorse that methodology! It looks very promising.By putting view information into the model (in the ''Nub''), you might be committing the cardinal sin of mixing Model and View, and you *may* find yourself banished into MVC purgatory to consider what you''ve done... -- * J * ~
use the render partial collection counter to do your style alternation <%= render :partial => "listitem", :collection => @list %> provides you with listitem_counter to be able to use for alternating style in your partial like <tr class="<%= (listitem_counter mod 2) ? ''style1'' : ''style2'' %>">... You would put everything you need into the partial, if in your case the partial is shared and you need to use a partial elsewhere without the links then you might use another partial which includes this partial or simply some conditional logic around parts of it. On 8/2/06, Igor <imixaly4@gmail.com> wrote:> Hello everyone! > I have generic views for CRUD operations > For example I render(''shared/list'') from controller > > shared/list.rhtml > ... > <% for item in @list %> > <tr class="<%= cycle ''list-line-odd'', ''list-line-even'' %>"> > <%= render :partial => ''item'', :locals => { :item => item } %> > <td><%= link_to ''Edit'', :action => :edit, :id => item %></td> > <td><%= link_to ''Destroy'', { :action => :destroy, :id => item }, > :confirm => "Delete: #{item.name} ?", :post => true %></td> > </tr> > <% end %> > ... > > As you see I also need to render :partial => ''list'' on every iteration > to add some custom information for a particullar controller. > > districts/_item.rhtml > ... > <td><%= item.name %></td> > <td><%= item.region.name %></td> > ... > > As you may already guess I''m getting ActionController::DoubleRenderError > So is there better way of doing this, or maybe some other function > method such as: render_and_not_raise_DoubleRenderError exist? :) > Any ideas? > > Someone proposed to use render_to_string, but it does not work in views. > Or creating an array with items in controller that will be used in > template to populate the view. But its not MVC way of doing things. > > I mean common! It should be simple, as a method call! > > Thanks for help! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 8/2/06, Igor Su <imixaly4@gmail.com> wrote:> Its all working now! > Sorry, my bad! > It was hapening bacause I didn''t remove render method from my > controller. > This method was called first and then one from generic controller > > By the way is this http://dereksivers.com/rails-shared-controller.html > a good way for DRYing the CRUD controllers or there are better ways?I hadn''t seen that before and was pretty impressed by it as well. Taking inspiration from Derek''s example, I implemented something similar as a mixin module. I''m not sure it''s a good way of doing it, and it''s very rough, but it seems to work OK so far: http://rpaste.com/pastes/355 It doesn''t require you to define a ''model'' method because it guesses it automatically based on the controller it''s mixed in with (somewhat inelegantly): def _model_ self.class.to_s.match(/^(.*)Controller$/)[1].constantize end So all you have to do is something like: class DomainController < ApplicationController include InstantCrud end It also assigns instance variables in a nicer way, thanks to the inflector. Given the controller above, you get @domains in your list action and @domain in view and edit instead of @list, @edit etc. Probably you''re better of going with the REST features in edge rails though. That said, I''d like to know whether or not this is a horrible way of implementing something like this. -Pawel