go Rails 知识点,Concepts Series:url和parameter; 建立Rails App Templates

Rails Concepts Series: 

https://gorails.com/series/rails-concepts 

  1. 基本都是免费的
  2. 一些细小的知识点,很有帮助。
     



URL和parameter 的简单解析。

http://localhost:3000/products?test=111 

在routes.rb中

get '/products', to: "products#indx" 

Rails routes会解析url,提取一个参数: params[:test] = 111 

这个参数传给controller中的 Product#index:

@test =  params[:test]

在views/products/index.html.erb写:

<%= @test%> 

最后会在web page 上显示。 

http://localhost:3000/products/dd?test=111

在routes.rb中

get '/products', to: "products#index" 

get '/products/id', to: 'products#show' 

Rails routes会解析url,提取2个参数: id和test

这个2参数传给controller中的 Product#show:

@test =  params[:test]

@id = params[:id] 

在views/products/index.html.erb写:

<%= @test%> <%= @id%>

最后会在web page 上显示这2个参数

在termiinal的log上,会出现:Parameters: {"test" => "111",  "id" => "dd"} 



猜你喜欢

转载自www.cnblogs.com/chentianwei/p/9258155.html