link_to和其对应要跳转的的url,用path和直接路由方法

link_to和其对应要跳转的的url,用path和直接路由方法

看看link_to

<% @order.each do |oo| %>
  <div>
    <%= oo.name %>   <%= link_to 'see', xshow_order_path(oo),{:onclick => "alert(1)"} %><br/>
    <%= sanitize(oo.des) %>
  </div>
	
<% end %>

在rake routes中看到,定义好的路由是:

C:UsersAdministratorDesktoplianxizhonglianxi1_form_fordepot>bundle exec ra
ke routes
  orders_index GET    /orders/index(.:format)     orders#index
   xshow_order GET    /orders/:id/xshow(.:format) orders#xshow
xcreate_orders POST   /orders/xcreate(.:format)   orders#xcreate
        orders GET    /orders(.:format)           orders#index
               POST   /orders(.:format)           orders#create
     new_order GET    /orders/new(.:format)       orders#new
    edit_order GET    /orders/:id/edit(.:format)  orders#edit
         order GET    /orders/:id(.:format)       orders#show
               PUT    /orders/:id(.:format)       orders#update
               DELETE /orders/:id(.:format)       orders#destroy

  

所以xshow的path又可以这样写,其中{:controller => "orders",:id => oo, :action => "xshow"}中的顺序是不能错的

<% @order.each do |oo| %>
  <div>
    <%= oo.name %>   <%= link_to 'see', {:controller => "orders",:id => oo, :action => "xshow"} %><br/>
    <%= sanitize(oo.des) %>
  </div>
	
<% end %>

  

原文地址:https://www.cnblogs.com/juandx/p/3963077.html