---
# System prepended metadata

title: '[Rails4][path][routes]routes與路徑相關筆記'
tags: [Rails4, path, routes]

---

# [Rails4][path][routes]routes與路徑相關筆記

###### tags: `Rails4`,`path`,`routes`

## helper相關

### url_for
https://apidock.com/rails/ActionView/RoutingUrlFor/url_for


### 將path做為參數傳入link_to helper

#### view
```htmlmixed!
<%
extra_params={
  cat_id: 2,
  a: 10,
  b: 20,
  c: 30,

}

 path= Proc.new{|i| url_for(
    {
    controller: "products",
    action: "edit", 
    id:i,
    }.merge(extra_params)
    )}
%>
```
產生html如下:
```htmlmixed!
<a class="some class" href="/products/2/edit?a=10&b=20&c=30&cat_id=2">test link</a>
```


## 網址路徑相關

### request methods 查詢:
https://edgeapi.rubyonrails.org/classes/ActionDispatch/Request.html

### 取得網頁根網址
https://stackoverflow.com/questions/21321687/rails-get-app-root-base-url/29341735
```ruby
request.base_url
```

### 取得目前網頁網址(不含domain)
https://stackoverflow.com/questions/2165665/how-do-i-get-the-current-absolute-url-in-ruby-on-rails
```ruby
request.original_fullpath
```

### 取得目前網頁完整網址(包含domain)
```ruby
request.original_url
#或
+request.original_fullpath
```

### 取得相對路徑
https://stackoverflow.com/questions/20181216/how-to-get-the-current-route-in-rails
```ruby
request.path
```



---

## routes相關設定

>rails的routes位於config/routes.rb

### 設定首頁
```ruby=
root "welcome#index"
```

### 自訂傳入id參數名稱
```ruby=
resources :kkk,param: :kkk_id
```

產生路徑:
```terminal
 kkk_index GET    /kkk(.:format)                                  kkk#index
           POST   /kkk(.:format)                                  kkk#create
 new_kkk   GET    /kkk/new(.:format)                              kkk#new
 edit_kkk  GET    /kkk/:kkk_id/edit(.:format)                     kkk#edit
 kkk       GET    /kkk/:kkk_id(.:format)                          kkk#show
           PATCH  /kkk/:kkk_id(.:format)                          kkk#update
           PUT    /kkk/:kkk_id(.:format)                          kkk#update
           DELETE /kkk/:kkk_id(.:format)                          kkk#destroy
```

### 使用member產生帶參數的自訂路徑

config/routes.rb
```ruby
resources :test do

    get "t1",on: :member
    
    
    #上述寫法也可改為:
    #member do
    #    get "t1"
    #end
    
end
```

產生路徑:
```terminal
t1_test    GET    /test/:id/t1(.:format)                       test#t1    <==注意此行
test_index GET    /test(.:format)                              test#index
           POST   /test(.:format)                              test#create
new_test   GET    /test/new(.:format)                          test#new
edit_test  GET    /test/:id/edit(.:format)                     test#edit
test       GET    /test/:id(.:format)                          test#show
           PATCH  /test/:id(.:format)                          test#update
           PUT    /test/:id(.:format)                          test#update
           DELETE /test/:id(.:format)                          test#destroy
```

### 使用collection產生無參數的自訂路徑

config/routes.rb
```ruby
resources :test do
    get "t1",on: :collection
    #上述寫法也可改為:
    #collection do
    #    get "t1"
    #end
end
```

產生路徑:
```
t1_test_index GET    /test/t1(.:format)                          test#t1  <==注意此行
  test_index GET    /test(.:format)                              test#index
             POST   /test(.:format)                              test#create
    new_test GET    /test/new(.:format)                          test#new
   edit_test GET    /test/:id/edit(.:format)                     test#edit
        test GET    /test/:id(.:format)                          test#show
             PATCH  /test/:id(.:format)                          test#update
             PUT    /test/:id(.:format)                          test#update
             DELETE /test/:id(.:format)                          test#destroy
```

### member搭配only

config/routes.rb
```ruby=
  resources :cats ,only:[:index] do
  
    get "products",on: :member
      
    resources :subcats,only:[:index] do
      get "products",on: :member,param: :subcat_id
    end

  end

```

產生路徑:
```terminal!
        products_cat GET    /cats/:id/products(.:format)                 cats#products
 products_cat_subcat GET    /cats/:cat_id/subcats/:id/products(.:format) subcats#products {:param=>:subcat_id}
         cat_subcats GET    /cats/:cat_id/subcats(.:format)              subcats#index
                cats GET    /cats(.:format)                              cats#index
```
>與only相反的參數是except


### view可直接將物件做為參數傳入


假設路徑如下:
```terminal
products_cat GET    /cats/:id/products(.:format)                 cats#products
```

路徑是預設接受一個id參數,但在view中可以直接將物件做為參數傳入:
```htmlmixed=
<%= link_to products_cat_path(cat) do %>
  <%=cat.name%>
<% end%>
```

在cats_controller內的products action使用render plain:params檢視參數,
印出的結果如下:

```htmlmixed=
{"controller"=>"cats", "action"=>"products", "id"=>"1"}
```


若需要傳入兩個id參數,可傳入兩個物件:
```htmlmixed=
<%= link_to products_cat_subcat_path(cat,subcat) do %>
  <%=subcat.name%>
<% end %>
```

檢視參數結果:

```htmlmixed=
{"controller"=>"subcats", "action"=>"products", "cat_id"=>"1", "subcat_id"=>"2"}
```

### 自訂path名稱:使用as
https://stackoverflow.com/questions/1436309/how-to-define-a-custom-path-in-rails

```ruby=
post "/ttt/:id/action",to: "conrtoller_name#action_name",as: "ttt_action"
```
上面的路由path名稱為ttt_action_path

### 顯示特定routes
```c!
$ rake routes | grep keywords_u_wanna_search
```


## 轉址相關

### 轉至特定網址
