Rails 3:如何使用 gsub 或用“-”替换空白字符?
我有一个艺术家模型是名称:字符串。我想要 /users/1/artists/jimi-hendrix/posts 而不是我现在拥有的 /users/1/artists/1/posts
问题是我不认为我可以使用Friendly_id作为艺术家的名字,这是因为我有多个相同的艺术家姓名,但我想使用相同的 slug,例如所有“jimi hendrix”条目都使用 jimi-hendrix。示例:
/users/3/artists/jimi-hendrix/posts
/users/55/artists/jimi-hendrix/posts
/users/106/artists/jimi-hendrix/posts
Friendly_id 使其看起来像这样:(我不能拥有) /用户/3/艺术家/jimi-hendrix/帖子 /用户/55/艺术家/jimi-hendrix--2/posts /users/106/artists/jimi-hendrix--3/posts
所以我想做的是将艺术家姓名参数而不是 id 传递给控制器。但我需要获取名称并将所有空格替换为“-”,然后在控制器中添加回空格,正确吗?
这将是我的链接:(你能做一些像artist.name.gsub!()这样的事情吗??)
<% @artists.each do |artist| %>
<%= link_to artist.name, user_artist_posts_path(@user, artist.name) %>
<% end %>
然后当我将代码返回到控制器时反转它?
定义索引 @name = params(:artist_id).gsub() # ? @帖子= ... .... 或者有人知道如何在Friendly_id
中包含非唯一的slugs,这样如果属性有重复的名称,它就不会附加--2、--3?
I have an Artist model is name:string. and I want /users/1/artists/jimi-hendrix/posts instead of what I have now which is /users/1/artists/1/posts
The problem is I don't think I can use friendly_id for the artist's name, this is because I have multiples of the same artist name but I want to use the same slug, such as jimi-hendrix for all 'jimi hendrix' entries. Example:
/users/3/artists/jimi-hendrix/posts
/users/55/artists/jimi-hendrix/posts
/users/106/artists/jimi-hendrix/posts
Friendly_id makes it looks like this: (which I can't have)
/users/3/artists/jimi-hendrix/posts
/users/55/artists/jimi-hendrix--2/posts
/users/106/artists/jimi-hendrix--3/posts
So what I'm thinking of doing is passing that artist name parameter to the controller instead of the id. But I need to take the name and replace all whitespace with "-" and then add back the whitespace in the controller correct?
This woulds be my link: (can you doing something like artist.name.gsub!() ??)
<% @artists.each do |artist| %>
<%= link_to artist.name, user_artist_posts_path(@user, artist.name) %>
<% end %>
Then reverse it when I get the code back to the controller?
def index
@name = params(:artist_id).gsub() # ?
@posts = ...
....
end
Or would anyone know how to have non-unique slugs in friendly_id, so it doesn't append the --2, --3 if the attribute has a duplicate name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要 gsub 任何非单词字符:
To gsub anything that is a non-word-character:
您应该能够为此使用范围化的Friendly_id。
这在Friendly_id 3.x 和4.x 中都可用。
例如:在Friendly_id 3.x中你可以
这样做,在4.x中
如果你的user_id是55,你的find语句应该是
User.find(55).artists.find(params[:id])
例如。或Artist.find(params[:id]).where(:user_id => 55)
。没有看到您可能拥有的其他代码(路线/模型/has_many/belongs_to关系)
我无法给你确切的答案。
FriendlyId 4.x 文档 http://rubydoc.info/github/norman/Friendly_id/master/框架
FriendlyId 3.2.1.1指南https://github.com/norman/friend_id/blob/a9505acb68c56719d8225ccb09b5840e26be2783/Guide.md
You should be able to use scoped friendly_id's for this.
This is available in both friendly_id 3.x and 4.x.
eg: in friendly_id 3.x you can do
and in 4.x
If your user_id is 55, your find statements should then be
User.find(55).artists.find(params[:id])
for example. ORArtist.find(params[:id]).where(:user_id => 55)
.Without seeing other code you may have (routes / models / has_many / belongs_to relationships)
I can't give you an exact answer.
FriendlyId 4.x docs http://rubydoc.info/github/norman/friendly_id/master/frames
FriendlyId 3.2.1.1 Guide https://github.com/norman/friendly_id/blob/a9505acb68c56719d8225ccb09b5840e26be2783/Guide.md