Ruby on rails 为rails创建数组url\u选项
我的rails应用程序post的默认url为_http://0.0.0.0:3000/posts/7,如何显示这样的url_http://0.0.0.0:3000/posts/cer4235235ft5435rerjk343f 使用数字和文本。在数据库中创建另一列,用字母数字字符串更新它,并使用该列进行查询 例如: 将列Ruby on rails 为rails创建数组url\u选项,ruby-on-rails,ruby,Ruby On Rails,Ruby,我的rails应用程序post的默认url为_http://0.0.0.0:3000/posts/7,如何显示这样的url_http://0.0.0.0:3000/posts/cer4235235ft5435rerjk343f 使用数字和文本。在数据库中创建另一列,用字母数字字符串更新它,并使用该列进行查询 例如: 将列permalink添加到posts表中 class Post < ActiveRecord::Base before_create :create_permalink
permalink
添加到posts表中
class Post < ActiveRecord::Base
before_create :create_permalink
def create_permalink
self.permalink = SecureRandom.hex
end
..
这是什么?
cer4235235ft5435rerjk343f
,你从哪里得到的?像自动生成一样。@Paritosh Piplewar,CER4235235FT5435RERJK34F我可以生成url的方法是什么。您想显示uuid
而不是id
?CER4235235FT5435RERJK34F
的任何原因都只是驻留在数据库中或可以动态计算的id或对象。看看这个,对你很有帮助控制器有什么变化吗?def set_post,我添加了,但是它的show post添加了一些文本config/routes.rb您可以使用常规id(它仍然是主键)来处理关系。对于此更改,您不需要对路由进行任何更改。我想在rails中的某个地方添加require'securerandom'
?
<%= link_to post.title, post_path(:id => post.permalink) %>
def show
@post = Post.where(:permalink => params[:id]).first
end