开发者

Should I put this in my controller or model?

I have my attend method below currently in my controller. My question is how do I k开发者_StackOverflow中文版now whether to put this in my event or user model vs. my events controller? I am also going to add another method remove_attend which will do the opposite of attend. At what point do I put these methods into a model?

 def attend
    @event = Event.find(params[:id])
    if @event.users.include?(current_user)
      flash[:error] = "You're already attending this event."
    else
      current_user.events << @event
      flash[:success] = "Attending event!"
    end
    redirect_to @event
  end


It belongs to the event controller. Flash messages or redirects cannot be put in the models. So, when you see either, it's safe to assume that they are controller material.

It belongs to the event controller because the resource that attend refers to is an Event. You can create, edit or attend an event, in that sense.


I'd leave this code in the controller. If you start accessing User attributes in this code (e.g. checking the type of user or the number of events they're already attending), then it might be a good idea to move that code into the user model.


This should be in the controller, the model should only be called upon to edit or retrieve data.

from what i'm seeing it looks like you're referencing an object (current_user) that isn't a property of the class you calling the method from, this shouldn't happen in the model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜