【发布时间】:2013-02-15 10:47:18
【问题描述】:
即使在使用 slug 之后,URL 仍然显示 id 而不是标题。 代码如下
index.html.erb
<title>Blog!</title>
<h1>List of the Posts</h1>
<% @posts.each do |post| %>
<%= link_to post.title,:id => post.slug%>
<p><%= post.content %></p>
<%= link_to "Edit",edit_post_path(post) %> |
<%= link_to "Delete",post,:confirm=>"Are you sure ?",:method=>:delete %>
<hr />
<% end %>
<p><%= link_to "Add a New Post",new_post_path %></p>
posts_controller.rb
class PostsController < ApplicationController
def index
@posts=Post.all
end
def show
@posts=Post.find(params[:id])
end
结束
后模型
extend FriendlyId
friendly_id :title,use: :slugged
def should_generate_new_friendly_id?
new_record
end
routes.rb
Blog::Application.routes.draw do
get "blog/posts"
resources :posts
end
我希望链接是 'localhost:8080/posts/this+is+the+title' 而不是 'localhost:8080/posts/2'
【问题讨论】:
-
是格式错误还是您在控制器中执行
extend等?
标签: ruby-on-rails-3 friendly-id