Forum: Ruby on Rails Shared code for controllers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3bec49196b296ac7c201412642c7a2b1?d=identicon&s=25 =?ISO-8859-1?Q?Rodrigo_Alvarez_Fern=E1ndez?= (Guest)
on 2005-12-16 12:28
(Received via mailing list)
Hi,

I think that this is made from a plugin or something in /lib/, but i
really
don't know which is the better way:

I have this in my MediasController:

  def mediatype_medias_options
    if params[:id]
      begin
        @mediatype = MediaType.find(params[:id])
        render :inline => "<%= options_from_collection_for_select @
mediatype.medias, 'id', 'name' %>"
      rescue Exception
        render :nothing => true
      end
    else
      render :nothing => true
    end
  end


I use it from a form, that sends a mediatype id, and this renders
<options>
for a select (it's something like a hier. select).
Since i must use this kind of thing into some controllers, i wish to
follow
the DRY principle, but don't know how.
Could you please give me some advice?

Thanks.
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2005-12-16 13:44
(Received via mailing list)
Put the code in:

app/controllers/application.rb

and it will be available to all controllers.

--
-- Tom Mornini
970efdae7d4dd77370743065457b17fe?d=identicon&s=25 Steven Mohapi-Banks (Guest)
on 2005-12-16 16:33
(Received via mailing list)
I'd just create a helper module in the controllers directory and then
require/include it in the controllers you need the method in.
This topic is locked and can not be replied to.