RailsのController&View

4905 ワード

railsが提供するツールでcontrollerを作成し、viewファイルとテストファイルを生成します.
root$ script/generate controller news list detail
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/news
      exists  test/functional/
      exists  test/unit/helpers/
      create  app/controllers/news_controller.rb
      create  test/functional/news_controller_test.rb
      create  app/helpers/news_helper.rb
      create  test/unit/helpers/news_helper_test.rb
      create  app/views/news/list.html.erb
      create  app/views/news/detail.html.erb

ルーティングの構成
vi config/routes.rb
  map.connect ':controller/:action'
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

コントローラを編集するには、次の手順に従います.
root$ cat app/controllers/news_controller.rb 
class NewsController < ApplicationController
  def list
    @newslist = [{"title"=>"new1","content"=>"text1"},{"title"=>"new2","content"=>"text2"}]
  end

  def detail
    id ||= params[:id]
    name ||=params[:name]
    @title = "new"+id + name
    @content = "text"+id + name
  end
end

viewテンプレートを編集するには、次のようにします.
root$ cat app/views/news/list.html.erb 
<h1>News#list</h1>
<p>Find me in app/views/news/list.html.erb</p>
<% @newslist.each do |news| %>
<p><%= news["title"] %></p>
<% end %>
root$ cat app/views/news/detail.html.erb 
<h1><%= @title %></h1>
<p><%= @content %></p>

次のリンクからリクエストを開始できます.
http://127.0.0.1:3000/news/list
http://127.0.0.1:3000/news/detail/1?name=ciaos
http://127.0.0.1:3000/news/detail?id=1&name=ciaos

次の方法でviewでcontrollerの呼び出しを実現できます.
#    controller   action  
<%= link_to "self_controller.otheraction", "otheraction" %>
#    controller action  
<%= link_to "another_controller.action", "/another_controller/action" %>
#      
<%= link_to "other sites", "http://www.baidu.com" %>

<%= link_to "link1", :action => "action", :id=> 3 %>
#http://127.0.0.1:3000/loc/act?id=3
<%= link_to "link2", :controller => "con", :action => "act", :page=> 3 %>
#http://127.0.0.1:3000/con/act?page=3
<%= link_to "link3", :controller => "con", :action => "act", :id=> 3, :page=>4 %>
#http://127.0.0.1:3000/con/act?id=3&page=4

コントロールでカスタムテンプレートlayout「StandardLayout」を構成すると、テンプレートはディレクトリの下のすべてのcssファイルとjsファイルを自動的にロードします.指定したファイルをロードする必要がある場合は、次の方法でロードできます.
  <%= stylesheet_link_tag    "kangli/style", "kangli/custom" %>
  <%= javascript_include_tag "jquery", "jquery_ujs", "kangli" %>

さらにcontrollerのrenderおよびredirectの使用例は以下の通りです.
class TestController < ApplicationController
	def index
		@user = {:name=>params[:name]}
		render :text => @user
=begin
		{:name=>:ciaos}
=end
		#render :xml => @user.to_xml
=begin
		<?xml version="1.0" encoding="UTF-8"?>
		<hash>
			<name type="symbol">ciaos</name>
		</hash>
=end 
		#render :json => @user.to_json
=begin
		{"name":"ciaos"}
=end
		#render :nothing => true
		/empty/
	end
	
	def show
		
		#redirect_to :action => "index", :name => params[:name] #, :age => 26 , ...
		
		redirect_to :controller => "main", :action => "index" #, :para => "ciaos"
		
		#redirect_to :back
		/go to previous page/
	end
end

http://ihower.tw/rails3/actioncontroller.html参照
Rails内蔵はHTTP Basic Authenticateをサポートし、簡単に検証を実現する
class PostsController < ApplicationController
    before_filter :authenticate

    protected

    def authenticate
     authenticate_or_request_with_http_basic do |username, password|
       username == "foo" && password == "bar"
     end
    end
end