dyserone dyserone - 1 year ago 34
Ruby Question

Rails, is it a good practice to create a method without a view?

I am relatively new to Rails, so I still have a lot of questions. I am creating an administration panel right now.

I have a model AdminUser, a folder admin_users in my views with 2 views only, dashboard and index and an admin_users_controller which is:

class AdminUsersController < ApplicationController

def dashboard


def index


def login
if params[:admin_user][:username].present? && params[:admin_user][:password].present?
found_user = AdminUser.where(:username => params[:admin_user][:username]).first
if found_user

authorized_user = found_user.authenticate(params[:admin_user][:password])
if authorized_user

redirect_to :controller => 'admin_users', :action => 'dashboard'


render :nothing => true, :status => :ok


Although I have a method for login, I do not have a view for it, because I don't really need it.

But the fact that Rails searches for a view make me think that I am doing something wrong; or at least not doing something the Rails-y way.

Should I do something in another way?
I would love to hear your suggestions.

Thank you

Answer Source

You are not required to have a view. In fact quite a few good reasons exist to not need a view:

  • actions that redirect before returning, such as the standard Rails create and update actions
  • actions that render inline, such as CSV generators
  • actions that render ajax results
  • actions that are endpoints for REST APIs

The conventions are there for the most common circumstances, but there are also conventions for lesser common, and still valid situations.

Note that methods that are not intended to be renderable actions should be placed in the protected or private section of the class to keep them separated from actual actions. Don't expose any methods in the public interface of the controller, except those that are intended to be actions.