How to Render a Layout Directly from Routes.Rb, Without a Controller

Render layout without a controller

I get that you don't need the controller to do anything, but Rails is "opinionated" software; it expects a controller and a view, because that is the way it was designed, and trying to work around that is going to give you a lot of trouble.

Just

  1. create an empty controller class in /app/controllers/main_controller.rb
  2. create an empty view file /app/views/main/index.html.erb
  3. set up a route like :root => 'main#index'

Easy peasy.

rails view without a controller

No. All requests has to go through a controller.

I like to have a PagesController, with map.page ":action", :controller => "pages". That way, I can create app/views/pages/foo.erb and have it available on /foo without any extra code.

ruby on rails how to render a page without layout and other head field

You can do it like this:

format.html { render "tabelle/show", :layout => false  } 

Rails & Devise: How to render login page without a layout?

You can subclass the controller and configure the router to use that:

class SessionsController < Devise::SessionsController
layout false
end

And in config/routes.rb:

devise_for :users, :controllers => { :sessions => "sessions" }

You need to move the session views to this controller too.

OR make a method in app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base

layout :layout

private

def layout
# only turn it off for login pages:
is_a?(Devise::SessionsController) ? false : "application"
# or turn layout off for every devise controller:
devise_controller? && "application"
end

end

Render remote view in local layout

Like requesting that information in a controller and then outputting it as a string in an ERB template?

Is there some other requirement that I'm missing?

Controller

def index
@content = body_of_request_to_service
end

View

...
<%= @content %>
...

But, I wouldn't recommend this style of microservices using Ruby (unless you have a dev-team that understands how to handle async Ruby IO). A good rule of thumb is to only have one blocking service request per client request.

Not Rendering the Default Application Layout in Rails 3

By Default, a controller will first look for a template with the
same name inside the layouts folder and if not found, it will
render the default application layout.

In this scenario, when home_controller index action is performed, the layout rendered is home.html.rb along with the view content since home.html.rb is with the same naming format as of the controller. When I changed it to homes.html.rb, the controller takes application.html.rb from the layouts folder.
So the solution is,

Just remove home.html.erb from views/layouts/ folder. It will
automatically render application.html.erb by default.

Thanks everyone.

Ruby on Rails: Can I route a request directly to a view?

No, you can't. Why?

Design: It's just a violation of the MVC pattern Rails enforces you to use, for your own good. There is always a controller involved. And yes, even for such stub pages, a controller is required. Anyway, a few lines of code won't hurt you, and you'll love it again when you need to perform some access control.

Hope that answered your question :-)

Rails controller action without a view

Just created another controller with the dislike action:

def dislike
@picture = Picture.find(params[:id])
@like = Like.find(:user_id => current_user.id, :picture_id => params[:id])

@like.destroy

respond_to do |format|
format.html { redirect_to :back, notice: 'You don\'t like this picture anymore.' }
format.json { render json: @picture }
end
end

and modified my routes.rb to match this action:

match 'pictures/:id/dislike' => "likes#dislike", :via => :post

and my link to dislike now is

<%= link_to 'Dislike!', {:action => :dislike, :controller => :likes}, :method => :post %>


Related Topics



Leave a reply



Submit