link_to 有多种方式:
1、
link_to(body, url, html_options = {})
2、
link_to(body, url_options = {}, html_options = {})
3、
link_to(options = {}, html_options = {}) do
# name
end
4、
link_to(url, html_options = {}) do
# name
end
link_to aritcle.title, article => href = /articles/1
link_to aritcle.title, article, id: 'article', class: 'article', target: '_blank', data: {item: article.id}=> href = /articles/1 id="articles" class="articles" target="_blank" data-item="1"
link_to aritcle.title, controller: "articles", action: "show", id: @article => href = /articles/show/1
<%= link_to [:mobile, article] do %>
<%= show_image article.image, w:200, h:160 %> #show_image为自定义方法
<% end %>
如果路由有namespace, 如:
namespace :mobile do
resources :articles, only: [:show]
end
以第一个link_to为例:
link_to article.title, [:mobile, article]
第二种写法
namespace :admin do
resources :articles, :comments
end
HTTP 方法 | 路径 | 控制器#动作 | 具名辅助方法 |
---|---|---|---|
GET | /admin/articles | admin/articles#index | admin_articles_path |
GET | /admin/articles/new | admin/articles#new | new_admin_article_path |
POST | /admin/articles | admin/articles#create | admin_articles_path |
GET | /admin/articles/:id | admin/articles#show | admin_article_path(:id) |
GET | /admin/articles/:id/edit | admin/articles#edit | edit_admin_article_path(:id) |
PATCH /PUT | /admin/articles/:id | admin/articles#update | admin_article_path(:id) |
DELETE | /admin/articles/:id | admin/articles#destroy | admin_article_path(:id) |