【发布时间】:2014-09-01 05:56:43
【问题描述】:
我有 User 和 Campaign 模型。我在用户和 URL 下嵌套广告系列如下所示:
http://localhost:3000/user-slug/campaign-slug
路线:
resources :users, :path => '' do
resources :campaigns, :path => ''
end
广告系列模型:
class Campaign < ActiveRecord::Base
...
extend FriendlyId
friendly_id :title, use: :history
...
end
我的用户模型不使用历史记录。
广告系列控制器(来自friendly_id 指南):
class CampaignsController < ApplicationController
before_filter :find_campaign
def show
@campaign = Campaign.friendly.find(params[:id])
end
private
def find_campaign
@campaign = Campaign.friendly.find(params[:id])
# If an old id or a numeric id was used to find the record, then
# the request path will not match the post_path, and we should do
# a 301 redirect that uses the current friendly id.
if request.path != campaign_path(@campaign)
return redirect_to @campaign, :status => :moved_permanently
end
end
end
当我访问旧 slug 以触发重定向时,我收到此错误:
ActionController::UrlGenerationError in CampaignsController#show
No route matches {:action=>"show", :controller=>"campaigns", :format=>nil, :id=>nil, :user_id=>#<bunch of other stuff in here>} missing required keys: [:id]
不确定我应该如何调整重定向方法以使其正常工作。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 nested-resources friendly-id