rails 杂记 - erb 中的 link_to 的 ActiveRecord 与 render 中的 partial

路由及路由参数

<%= link_to 'My Blog', {controller: 'articles', demo: "lidsi"}, class: "blogs", id: "blogs" %>
指向 http://0.0.0.0:3000/articles?demo=lidsi
<a class="blogs" id="blogs" href="/articles?demo=lidsi">My Blog</a>

在 {} 里的内容被当作query参数,在{}外面的被当作 html属性。

 

将 ActiveRecord 作为 link_to 或 redirect_to 对象 (参考:http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects)

<%= link_to 'Ad details', [@magazine, @ad] %>

<%= link_to 'Ad details', url_for([@magazine, @ad]) %>


<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %>

controller 是这样的

resources :magazines do
  resources :ads
end

helper 会生成一个 route类似

 article_comment GET    /articles/:article_id/comments/:id(.:format)      comments#show

又如

<%= link_to 'article details', @article %>

指向 article GET    /articles/:id(.:format)       articles#show

@articles 指向  articles GET    /articles(.:format)       articles#index

 

在 erb中 render 跟 partial 有关,而 link_to 才跟 controller 有关。 

 render @object 或 render @collection 请

参考 http://www.cnblogs.com/lemos/p/8493366.html

end

 

转载于:https://www.cnblogs.com/lemos/p/8490239.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值