Shared code for controllers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Shared code for controllers

Rodrigo Alvarez Fernández
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.

_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Shared code for controllers

Tom Mornini
Put the code in:

app/controllers/application.rb

and it will be available to all controllers.

--  
-- Tom Mornini

On Dec 16, 2005, at 6:27 AM, Rodrigo Alvarez Fernández wrote:

> 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.
> _______________________________________________
> Rails mailing list
> [hidden email]
> http://lists.rubyonrails.org/mailman/listinfo/rails

_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Shared code for controllers

Steven Mohapi-Banks
In reply to this post by Rodrigo Alvarez Fernández
I'd just create a helper module in the controllers directory and then  
require/include it in the controllers you need the method in.


On 16 Dec 2005, at 11:27, Rodrigo Alvarez Fernández wrote:

> 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.
> _______________________________________________
> Rails mailing list
> [hidden email]
> http://lists.rubyonrails.org/mailman/listinfo/rails

_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails