Tutorial by Examples

class UsersController < ApplicationController def index hashmap_or_array = [{ name: "foo", email: "[email protected]" }] respond_to do |format| format.html { render html: "Hello World" } format.json { render json: hashmap_or_array } en...
class UsersController < ApplicationController def index respond_to do |format| format.html { render html: "Hello World" } end end end This is a basic controller, with the addition of the following route (in routes.rb): resources :users, only: [:index] Will...
Controllers have access to HTTP parameters (you might know them as ?name=foo in URLs, but Ruby on Rails handle different formats too!) and output different responses based on them. There isn't a way to distinguish between GET and POST parameters, but you shouldn't do that in any case. class UsersCo...
class UsersController < ApplicationController def index respond_to do |format| format.html do render html: "Hello #{ user_params[:name] } user_params[:sentence]" end end end private def user_params if params[:name] == "john&quo...
Assuming the route: resources :users, only: [:index] You can redirect to a different URL using: class UsersController def index redirect_to "http://stackoverflow.com/" end end You can go back to the previous page the user visited using: redirect_to :back Note that i...
Assuming the route: resources :users, only: [:index] And the controller: class UsersController < ApplicationController def index respond_to do |format| format.html { render } end end end The view app/users/index.html.erb will be rendered. If the view is: Hello &lt...
Rescue from record not found error instead of showing an exception or white page: class ApplicationController < ActionController::Base # ... your other stuff here rescue_from ActiveRecord::RecordNotFound do |exception| redirect_to root_path, 404, alert: 'Record not found' en...
class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] def index @posts = Post.all end def show end def new @post = Post.new end def edit end def create @post = Post.new(post_par...
If you want to display to your users meaningful errors instead of simple "sorry, something went wrong", Rails has a nice utility for the purpose. Open the file app/controllers/application_controller.rb and you should find something like this: class ApplicationController < ActionContro...
Filters are methods that are run "before", "after" or "around" a controller action. They are inherited, so if you set any in your ApplicationController they will be run for every request your application receives. Before Filter Before filters are executed before the c...
Rails provides a lot of generators, for controllers too of course. You can generate a new controller by running this command in your app folder rails generate controller NAME [action action] [options] Note: You can also use rails g alias to invoke rails generate For example, to generate a cont...
You can rescue a RecordNotFound exception with a redirect instead of showing an error page: class ApplicationController < ActionController::Base # your other stuff rescue_from ActiveRecord::RecordNotFound do |exception| redirect_to root_path, 404, alert: I18n.t("errors.record...

Page 1 of 1