*ブログに戻る →ここ です。
参考サイトは下記にリンクを張っています。

Rubyの学習 free222
メソッド「redirect_to」を使用することで、違うアクションへ
元のコントローラから別のコントローラのアクションへ転送
違うサイトへ転送ができる。

↓Rubyのプログラムを実行すると以下のように表示される。

デフォルトだと
URL://localhost:3000/hiraya_plans/src1

HirayaPlans#src1

Find me in app/views/hiraya_plans/src1.html.erb ---------------------------------------------------- URL//localhost:3000/hiraya_plans/src1

HirayaPlans#dest

Find me in app/views/hiraya_plans/dest.html.erb --------------------------------------------------- URL//localhost:3000/hiraya_plans/src2

HirayaPlans#dest

Find me in app/views/hiraya_plans/dest.html.erb --------------------------------------------------- URL//localhost:3000/hiraya_plans/src3 http://psdrawingman.com/へ転送される --------------------------------------------------- URL//localhost:3000/hiraya_plans/src4 URL//localhost:3000/yoshi_movies/index へ転送される

YoshiMovies#index

現在地:福山市 郵便番号:72901 本籍:Japan 名前:DrawingMother 年齢:1 Find me in app/views/yoshi_movies/index.html.erb --------------------------------------------------- URL//localhost:3000/hiraya_plans/dest

HirayaPlans#dest

Find me in app/views/hiraya_plans/dest.html.erb

Saturday,November,30,2013

プログラムの表示

すでに作成しているアプリケーション名「kittakasample」へ、 「HirayaPlans」コントローラを作成し、「src1」「src2」「src3」「dest」アクションを同時に作成する。 ↓アクション↓アクション名 rails generate controller HirayaPlans src1 src2 src3 src4 dest ↑アクション↑アクシ ↑アクション名 ↑コントローラー名(ファイル名hiraya_plans_controller.rb) cd C:\SadaData\rails\kittakasample C:\SadaData\rails\kittakasample>rails generate controller HirayaPlans src1 src2 src3 src4 dest ビューを確認する C:\SadaData\rails\kittakasample\app\views\hiraya_plans 標準ではインデックスは作成されないんですね dest.html.erb src1.html.erb src2.html.erb src3.html.erb src4.html.erb ----------------------------------------------------- Kittakasample/config/routes.rb を開いて確認する Kittakasample::Application.routes.draw do get "yoshi_movies/index" get "ishi_photos/index" get "ishi_photos/kitshow" get "hiramemo/new" get "hiramemo/show" Kittakasample::Application.routes.draw do ↓コントロール名は「HirayaPlans」 get "hiraya_plans/src1" ←ブラウザからアクセスする場合フォルダ名「hiraya_plans」を指定してアクション「src1」を指定している get "hiraya_plans/src2" ↑URL://localhost:3000/hiraya_plans/src1.html.erb get "hiraya_plans/src3" get "hiraya_plans/src4" get "hiraya_plans/dest" get "yoshi_movies/index" get "ishi_photos/index" get "ishi_photos/kitshow" get "hiramemo/new" get "hiramemo/show" ---------------------------------------------- C:\SadaData\rails\kittakasample\app\controllers\hiraya_plans_controller.rb が作成される。 app/controllers/hiraya_plans_controller.rb をさくらエディタで開く class HirayaPlansController < ApplicationController def src1 end def src2 end def src3 end def src4 end def dest end end やっぱりデフォルトだとindex は作成されていない ----------------------------------------------------- C:\SadaData\rails\kittakasample\app\controllers\hiraya_plans_controller.rb をさくらエディタで開いて加筆する。 =begin ファイルの保存はUTF-8でおこないます デフォルトのコントローラからアクション指定する場合 redirect_to :action => 'アクション名' 元のコントローラから別のコントローラーのアクションに転送する場合 redirect_to :controller => 'コントローラ名' :action => 'アクション名' Rubyのコメントのテストもおこなった =end # coding: utf-8 class HirayaPlansController < ApplicationController def src1 #302は一時的に移動、301は恒久的、だけどこれ「コマンドプロンプトからcurl」が分からないので流す redirect_to :action => 'dest', :status => 301 #コメントテスト(app/views/hiraya_plans/src1.html.erb) end #→(app/views/hiraya_plans/dest.html.erb) def src2 redirect_to '/hiraya_plans/dest' #コメントテスト(app/views/hiraya_plans/src2.html.erb) end #→(app/views/hiraya_plans/dest.html.erb) def src3 redirect_to 'http://www.psdrawingman.com/' #コメントテスト(app/views/hiraya_plans/src3.html.erb) end #コメントテスト(URLへ移動) def src4 redirect_to :controller => 'yoshi_movies', :action => 'index' #コメントテスト(app/views/yoshi_movies/index.html.erb) end #↑前回作ったコントローラ名 #大小文字の「YoshiMovies」コントローラー名のフォルダはこのようになる。 def dest end end #お手本は元のコントローラーから別のコントローラに移動する時にカンマ入れてないからエラーになる ------------------------------------------------------------- 「kittakasample」アプリケーションを実行する アプリケーションの起動 ↓アプリ名[kittakasample] C:\SadaData\rails\kittakasample>rails server ブラウザからURLへアクセス ウェブブラウザからアクセスする(前回作ったコントローラ名YoshiMovies) URL://localhost:3000/yoshi_movies/index URL//localhost:3000/yoshi_movies/index.json URL//localhost:3000/yoshi_movies/index.xml

YoshiMovies#index

現在地:福山市 郵便番号:72901 本籍:Japan 名前:DrawingMother 年齢:1 Find me in app/views/yoshi_movies/index.html.erb ------------------------------------------------- 「kittakasample/app/views/yoshi_movies/new.html.erb」をさくらエディタを開き加筆する <h1>YoshiMovies#index</h1> <p> 現在地:<%= @_sadaPersonal["Location"] %><br /> 郵便番号:<%= @_sadaPersonal["Zip"] %><br /> 本籍:<%= @_sadaPersonal["本籍"] %><br /> 名前:<%= @_sadaPersonal["name"] %><br /> <!--メモ、順番入れ変えてみた エラーが出た、変数を間違っていたので保存形式ではなかった、保存形式をUTF-8に変えた--> 年齢:<%= @_sadaPersonal["old"] %><br /> <!--保存形式をEUCへ変更したらエラーとなったのでUTF-8に戻した--> </p> <p>Find me in app/views/yoshi_movies/index.html.erb</p> ---------------------------------------------------------- URL://localhost:3000/hiraya_plans/src1 ←ブラウザからアクセスする場合フォルダ名「hiraya_plans」を指定してアクション「src1」を指定している URL://localhost:3000/hiraya_plans/src2 ↑URL://localhost:3000/hiraya_plans/src1.html.erb URL://localhost:3000/hiraya_plans/src3 URL://localhost:3000/hiraya_plans/src4 URL://localhost:3000/hiraya_plans/dest 下記のアドレスへ転送しようと試みたが出来なかった URL//localhost:3000/yoshi_movies/index.json URL//localhost:3000/yoshi_movies/index.xml


*↓ 参考にしたサイトは下記
こちら です。

これは赤色の文字例です。

これは青色の文字例です。