我正在尝试建立一个类似新闻网站的网站。用户可以发布文章,这些文章会被归类到不同的类别,例如体育、政治等。
想法是有一个索引 Controller ,其中包含 home/sports/politics... 使 url 像 xxx.com/index/home、xxx.com/index/sports 的方法。主页将显示每篇文章。但是,我在尝试获取数据时收到了 ActiveRecord::RecordNotFound 消息。
目前有 3 个 Controller 。 1.索引 2. 用户(使用设计) 3. Post(带post模型)
在索引 Controller 中:
class IndexController < ApplicationController
before_filter :find_post
def home
end
def find_post
@user = User.find(params[:id])
@posts = @user.posts.find(params[:id])
end
end
在后期模型中:
class Post < ActiveRecord::Base
belongs_to :user
validates :content, presence: true
end
route.rb 包含
Rails.application.routes.draw do
devise_for :users
resources :posts
当我运行/index/home page 时,我收到了消息 ActiveRecord::RecordNotFound 在 IndexController#home 找不到“id”=
的用户提取的源代码(大约第 39 行):
37 def find_post
38
39 @user = User.find(参数[:id])
最佳答案
那是因为 params[:id] 是空的,因为 url 中没有 id。您应该相应地更改您的代码。
关于ruby-on-rails - 尝试从其他 Controller 模型检索数据时收到 ActiveRecord::RecordNotFound 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182027/