Rails: controllers

to create a new controller:

ruby script/generate controller yourcontrollernamehere

Now you just need to add a view file with the HTML under folder:

views/yourcontrolernamehere

For rails 3:

$ rails generate controller Pages home contact

This adds the controller pages, and then particular pages “home” and “contact” under that controller

This will also generate the needed routes (and modify the routes file accordingly), the RSpec and views placeholders as well.

For the routers, you can just define a resource for all of them:

resources :yourControllerName

You also need to define the methods inside your new controller. For example, in the “new” method, you could do something like:

def new
  @yourcontrollername = Yourcontrollername.new
end

Another example for “create”:

def create
  @project = Project.new(params[:project])
  @project.save
  flash[:notice] = "Project has been created."
  redirect_to @project
end

You will also need to generate your model manually (since you are not scaffolding)

If you want to undo the controller you just created, you can run the following command:

rails destroy  controller Pages home contact

Rails 4

The format to generate new controllers is:

rails generate controller ControllerName [actions] [options]

This usually generates an empty file inside /app/controllers/user_controller.rb, you need to fill out the actions to make it useful.

The first line populates the user for edit / delete purposes:

before_action :set_user, only: [:show, :edit, :update, :destroy]

It goes along with the following method:

private
  def set_user
    @user = User.find(params[:id])
  end

Here’s an example of how a typical set of action methods look inside the controller:

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to articles_path, notice: 'User successfully added.'
    else
      render action: :new
    end
  end

  def edit
  end

  def update
    if @user.update(user_params)
      redirect_to articles_path, notice: 'Updated user information successfully.'
    else
      render action: 'edit'
    end
  end

The following code (inside your controller) tells what attributes are safe to save / update (so users can’t compromise the system by sending those manually:

def article_params

params.require(:article).permit(:title, :location, :excerpt, :body, :published_at)

end

Notice that using the controller generator just puts the empty files in place, it is up to you to fill out the details (as opposed to what the scaffolding does for you, which is generating all the default forms and actions and content for you)

If you are using rails-api, and just serving JSON, here’s an example for the controllers:

  def index

    all_tasks = ScheduledReportTask.where(site_id: params[:site_id])

    render :json => {:status => “ok”, :response_code => 200, :message => “ok”, :results => all_tasks }.to_json

  end

  def show

    begin

      @scheduled_report_task = ScheduledReportTask.find(params[:id])

      if @scheduled_report_task.site_id == @global_site_id

        render :json => self.prepare_return(@scheduled_report_task)

      else

        self.deny_access

      end

    rescue

      render :json => self.package_error

    end

  end