Rails中的路由:错误的URL

时间:2011-08-21 16:31:58

标签: ruby-on-rails ruby-on-rails-3 routing

问题是当我点击链接时,URL看起来像:

/show?id=1&slug=aasdasd

但应该是:

/strona/1/aasdasd

在routes.rb

match "strona/:id/:slug", :controller => "subpages", :action => "show", :via => :get  

在application_controller.rb中:

def subpages
 Subpage.all
end

在application.html.erb中:

<% subpages.each do |subpage| %>
  <%= link_to subpage.title, {:controller => 'subpages', :action => 'show', :id => subpage.id, :slug => subpage.title.parameterize} %>
<% end %>

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

Routes.rb(为您的路径添加“:as =&gt;:strona”):

match "strona/:id/:slug", :controller => "subpages", :action => "show", :via => :get, :as => :strona

View.html.erb:

<%= link_to subpage.title, strona_path(subpage.id, subpage.title.parameterize) %>