MaryLu MaryLu - 1 year ago 48
Ruby Question

Rails aways renders the application.html.erb instead of wanted views

My Ruby on Rails application always renders the layouts/application.html.erb view instead the views I want it to. Has anyone an Idea why that might be so?
My routes file looks like this:

Rails.application.routes.draw do
root 'startup#index'
resources :users

And my application_cotroller.rb is pretty much empty:

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

Answer Source

By default, a controller action in Rails renders the view template for your action, wrapped up into a layout (which is application/layout)

ActionView::TemplateHandlers manages the lookup for the extension (.html.erb, .html.haml, .json.erb, etc ...)

so, in an action called index, you will get this implicit call unless you call render yourself :

def edit
  # your code
  render action :'edit', layout: 'application/layout' # implicitly called

Rails will then start processing your layout and put the content of your edit template in place of any yield within your layout. Thus, a typical layout will look like this :

<!doctype html>
  <!-- layout content before view -->

  <%= yield %>

  <!-- layout content after view -->