Rails之Controller&View

用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'

编辑controller如下

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

在controller中配置自定义模板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

你可能感兴趣的:(controller)