Ruby on rails 4 ActiveRecord::FavoritesController#create中的UnknownAttributeError
我正在获取收藏夹的未知属性“post”。当我在表中缺少一列时,通常会发生此错误。错误指出这部分是问题所在:favorite=current_user.favorites.build(post:post)。但不应该有另一个专栏叫post。我是否有另一套我丢失的嵌套参数,可能是在某些东西下面分层的?还是我的引用完整性不正确 错误屏幕截图 收藏夹\u controller.rb文件Ruby on rails 4 ActiveRecord::FavoritesController#create中的UnknownAttributeError,ruby-on-rails-4,activerecord,Ruby On Rails 4,Activerecord,我正在获取收藏夹的未知属性“post”。当我在表中缺少一列时,通常会发生此错误。错误指出这部分是问题所在:favorite=current_user.favorites.build(post:post)。但不应该有另一个专栏叫post。我是否有另一套我丢失的嵌套参数,可能是在某些东西下面分层的?还是我的引用完整性不正确 错误屏幕截图 收藏夹\u controller.rb文件 class FavoritesController < ApplicationController def
class FavoritesController < ApplicationController
def create
post = Post.find(params[:post_id])
favorite = current_user.favorites.build(post: post)
if favorite.save
flash[:notice] = "This post is now favorited."
redirect_to [post.topic, post]
else
flash[:error] = "There was an error favoriting you post. Please try again."
redirect_to [post.topic, post]
end
end
end
rake routes | grep -i favorites
post_favorites POST /posts/:post_id/favorites(.:format) favorites#create
post_favorite DELETE /posts/:post_id/favorites/:id(.:format) favorites#destroy
岗位控制员
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
has_many :votes
has_one :summary
belongs_to :user #means the post table has the user table's primary k ey in it
belongs_to :topic
has_many :favorites
#has_one :summary
mount_uploader :avatar, AvatarUploader
#default_scope {order('created_at DESC')}
default_scope {order('rank DESC')}
validates :title, length: {minimum: 5}, presence: true
validates :body, length: {minimum: 20}, presence: true
validates :topic, presence: true
validates :user, presence: true
def create_vote
# self == post
user.votes.create(value: 1, post: self)
end
def markdown_title
(render_as_markdown).render(self.title).html_safe
end
def markdown_body
(render_as_markdown).render(self.body).html_safe
end
def up_votes
votes.where(value: 1).count
end
def down_votes
votes.where(value: -1).count
end
def points
votes.pluck(:value).sum
end
def update_rank
age_in_days = (created_at - Time.new(1970,1,1)) / (60 * 60 * 24) #1 day in seconds
new_rank = points + age_in_days
update_attribute(:rank, new_rank)
end
private
def render_as_markdown
renderer = Redcarpet::Render::HTML.new
extensions = {fenced_code_blocks: true}
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
return redcarpet
end
end
收藏路线
class FavoritesController < ApplicationController
def create
post = Post.find(params[:post_id])
favorite = current_user.favorites.build(post: post)
if favorite.save
flash[:notice] = "This post is now favorited."
redirect_to [post.topic, post]
else
flash[:error] = "There was an error favoriting you post. Please try again."
redirect_to [post.topic, post]
end
end
end
rake routes | grep -i favorites
post_favorites POST /posts/:post_id/favorites(.:format) favorites#create
post_favorite DELETE /posts/:post_id/favorites/:id(.:format) favorites#destroy
这条线
favorite = current_user.favorites.build(post: post)
应该是
favorite = current_user.favorites.build(post_id: post.id)
因为您的收藏夹表中有
post\u id
而不是post
请发布错误。请尝试将favorite=current\u user.favorites.build(post:post)
更改为favorite=current\u user.favorites.build(post\u id:post.id)
是的,我之前尝试过,但没有成功。我得到一个错误:未定义的局部变量或方法“post\u id”用于#它必须是我正在寻找的小东西…这实际上应该可以工作favorite=current\u user.favorites.build(post\u id:post.id)
。你仔细检查了吗?