why I got the error “Couldn't find article with id=ni-hao-wo-zhen-de-henhaoma” when using stringex?

眉间皱痕 提交于 2020-01-17 04:45:08

问题


I want to use stringex for more friendly url. Now my steps as follows:

(1) In the model called article:

  acts_as_url :title, :url_attribute => :slug

  def to_param
   slug
  end

(2) articles#show:

def show
  debugger
  show! do |format|
  format.html # show.html.erb
     format.json { render json: @article }
  end
end

(3) the articles/_article.html.erb includes:

<%= link_to(article_url(article)) do %>
... 
<% end %>

and correctly generates html tag, such as:http://localhost:8000/articles/ni-hao-wo-zhen-de-henhaoma

When I click the link generated in (2), I got the error:

ActiveRecord::RecordNotFound in ArticlesController#show
Couldn't find Article with id=ni-hao-wo-zhen-de-henhaoma

I set a breakpoint in the entry of ArticlesController#show, but the above error come out before it.

What else steps should I do?

Updated: according to @jvnill's reminding, I think the backtrace maybe help:

activerecord (3.2.21) lib/active_record/relation/finder_methods.rb:344:in `find_one'
activerecord (3.2.21) lib/active_record/relation/finder_methods.rb:315:in `find_with_ids'
activerecord (3.2.21) lib/active_record/relation/finder_methods.rb:107:in `find'
activerecord (3.2.21) lib/active_record/querying.rb:5:in `find'
inherited_resources (1.4.1) lib/inherited_resources/base_helpers.rb:51:in `resource'
cancancan (1.10.1) lib/cancan/inherited_resource.rb:12:in `load_resource_instance'
cancancan (1.10.1) lib/cancan/controller_resource.rb:32:in `load_resource'
cancancan (1.10.1) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancancan (1.10.1) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'

回答1:


Please go through the guide first so you can understand the flow better.

http://guides.rubyonrails.org/v3.2.13/action_controller_overview.html#filters

To answer your error, what happens is that @article is being set in a before_filter and it most probably finds the record by id

@article = Article.find(params[:id])

Since params[:id] is the article slug, what you want to do is find by slug instead. So skip that before_filter for the show action and create another before_filter specifically for show action. Something like the following.

before_filter :fetch_article, except: :show
before_filter :fetch_article_by_slug, only: :show

private

def fetch_article
  @article = Article.find(params[:id])
end

def fetch_article_by_slug
  @article = Article.find_by_slug!(params[:id])
end

UPDATE

Using the inherited_resources gem, you may want to implement your own show action (the following code is untested and I can't vouch for it since I've never used the gem before)

actions :all, except: [:show]

def show
  @article = Article.find_by_slug!(params[:id])
end


来源:https://stackoverflow.com/questions/30683457/why-i-got-the-error-couldnt-find-article-with-id-ni-hao-wo-zhen-de-henhaoma-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!