更改to_param会在“model”_url中产生错误

时间:2011-04-07 15:37:22

标签: ruby-on-rails

Rails 2.3.8。

以下是to_param模型中的shop

  def to_param
    require 'unicode'
    "#{id}-#{Unicode::normalize_KD("-"+name+"-").downcase.gsub(/[^a-z0-9\s_-]+/,'').gsub(/[\s_-]+/,'-')[0..-2]}".gsub(/-{2,}/,'-')
  end

当我尝试将#{id}-更改为#{id}/以下内容时:

  def to_param
    require 'unicode'
    "#{id}/#{Unicode::normalize_KD("-"+name+"-").downcase.gsub(/[^a-z0-9\s_-]+/,'').gsub(/[\s_-]+/,'-')[0..-2]}".gsub(/-{2,}/,'-')
  end

我的index.html.erb

中出现以下错误
shop_url failed to generate from {:type=>"places", :action=>"show", :controller=>"shops", :id=>#<shop id: 16, shop_type: "fashion", name: "Shop1", shop_subtype: nil, ...}

Extracted source (around line #54):

51: 
52:               <% @shops.each do |shop| %>
53:                 <div id="<%= dom_id(shop) %>" class="item">
54:                   <a href="<%= shop_path(shop, :type => @type) %>">
55:                     <% if !shop.photos.blank? %>
56:                       <%= image_tag(shop.photos.last.url(:thumb), :class => 'thumbnail') %>
57:                     <% else %>

我正在尝试将网址从shops/12-shop-abc更改为shops/12/shop-abc。事实上,我实际上是尝试使用friendly_id更改为shops/shop-abc,但它在两者上都失败了。

请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

名称中的.会产生正常to_param的错误。它是用friendly_id解决的。