概要
Controllerからリダイレクトを行う
内容
リダイレクトの指定は下記を利用します。
redirect_to :controller => 'controller_name', :action => 'action_name'
仕様
Bookshelfページにリンクが2つあります。
「click link」リンクを押下するとリダイレクトにより
ヘルプページに遷移します。
サンプルコード
view/bookshelf/index.html.haml
%h1 Bookshelf#index %p Find me in app/views/bookshelf/index.html.haml %hr/ =form_tag("/help/index", method: "post") do %input{:type => "text", :name => "bookname"}/ %input{:type => "submit", :name => "submit", :value => "submit"}/ = link_to 'click_link', :controller => 'bookshelf', :action => 'click_link' = link_to 'message', :controller => 'bookshelf', :action => 'message'
view/help/index.html.haml
%h1 Help#index %p Find me in app/views/help/index.html.haml %div =@bookname
bookshelf_controller.rb
class BookshelfController < ApplicationController def index render layout: "html5_layout" end def click_link redirect_to :controller => 'help', :action => 'index' end def message render :text => "message test" end end
help_controller.rb
class HelpController < ApplicationController def index @bookname = params[:bookname] end end