Reputation: 3090
A controller method calls upon a model method to generate a token. Inside the controller method the generated token is also saved in a cookie: cookies.permanent[:remember_token] = user.remember_token
.
Would it not be better to include this last line in the model method that creates the token? It is DRYer since I have more than 1 controller with this same behaviour (using the same model method). And the risk that I would forget these cookies lines is lower. Or is it not possible to execute this command in a model method?
Update: I think it's not possible to do this in a model method because there it's not known to which user/computer to write the cookie to?
Upvotes: 0
Views: 334
Reputation: 18682
Models should have no knowledge about concepts like a user's cookie. It's really a controller thing.
If you're using the line across different controllers, you could move it into the ApplicationController
:
class ApplicationController < ActionController::Base
def store_remember_token(user)
cookies.permanent[:remember_token] = user.remember_token
end
end
And then reuse it in your controllers:
class SomethingsController < ApplicationController
def show
@user = # ...
store_remember_token(@user)
end
end
Upvotes: 2