简体   繁体   中英

ROR creating standalone components

In my web application I have a model User. It's quite common that you need to select some users for many different purposes related to many different models. My aim is to make this component very easy and fast to attach in a new place. Eg if a users wants to select his friends the result of selection should be handled by User controller, but if you want to assign some users to a task this should be handled by Task controller.

Do you have any concept how to do this? Should I make another controller for selecting? How should I pass the selection to the suitable controller? Maybe by session? Do you have any other suggestions?

I think what you are looking for is a module which has common methods. If so you can do something like:

Create a module called Users and add the methods to that, and keep it inside your lib folder

Ex:

<app root>/lib

module User
  def friends
    <returns the given users friends>
  end  
end

and then you can call this module in both your controllers and models

Ex:

Class Friend
  include User
end

Class FriendsController < ApplicationController
  include User
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM