Rails Concepts Series:
https://gorails.com/series/rails-concepts
- 基本都是免费的
- 一些细小的知识点,很有帮助。
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"}