【问题标题】:ActiveRecord::UnknownAttributeError in PostsController#showPostsController#show 中的 ActiveRecord::UnknownAttributeError
【发布时间】:2014-08-25 02:12:40
【问题描述】:
# posts_controller.rb
 def show
   @comment = @post.comments.build
  end!

我通过脚手架生成了 cmets_controller.rb 等。 然后就出现了这个错误。

我不知道该怎么做。 我该如何解决这个错误?

#comments_controller.rb
class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  # GET /comments
  # GET /comments.json
  def index
    @comments = Comment.all
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
  end

  # GET /comments/new
  def new
    @comment = Comment.new
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        format.json { render action: 'show', status: :created, location: @comment }
      else
        format.html { render action: 'new' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update

    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to comments_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:user_name, :body, :post_id)
    end
end


#posts_controller.rb
# GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
   @comment = @post.comments.build
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render action: 'show', status: :created, location: @post }
      else
        format.html { render action: 'new' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
        @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:date, :subject, :body, :picture)
    end

    def video
    end
end

【问题讨论】:

  • 我尝试修复您帖子的格式。请检查以确保一切都符合您的预期。
  • 问题似乎出在您的 Comment 模型中。你记得输入belongs_to :post吗? (注意单数,不是复数)。您确定在数据库表中创建了 post_id 列吗? (检查db/schema.rb)。给你这个错误的网址是什么?

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord


【解决方案1】:

在详细介绍之前只是一个建议。我假设您正在使用@post 在帖子页面上的表单中使用它。取而代之的是,您可以使用符号

<%= form_for :comment, method: :post, url: comments_path do |f| %>

或者,如果您使用嵌套路由

<%= form_for [@post, :comment], method: :post, url: comments_path do |f| %>

这将提交给 cmets 的控制器。

另外,通过检查您的 schema.rb 确保您的评论模型包含 post_id

【讨论】:

  • 您答案的最后一行可能是真正的问题。而且我总是建议在符号上使用实例。
  • 我知道您想要一个实例,因此回收了部分表单。我经常遇到的问题是,您最终会在控制器中拥有一大堆声明,而这些声明并没有真正做任何有用的事情。当我在页面上有大量新记录的表单时,我偶尔会使用符号。
【解决方案2】:

foriegn_key

问题将是您的comments 表不会有post_id 存在。

什么时候 你在 Rails 中使用ActiveRecord associations,虽然它看起来很神奇,但它依赖于老式的关系数据库架构。其中的主要组成部分是primary_keys(识别单个记录)和foreign_keys(识别关联记录)

--

如果你希望能够将两个模型关联在一起,你需要确保你有对应的foriegn_keys:

#app/models/post.rb
Class Post < ActiveRecord::Base
   #fields id | title | body | created_at | updated_at
   has_many :comments
end

#app/models/comment.rb
Class Comment < ActiveRecord::Base
   #fields id | post_id | other | attributes | created_at | updated_at
   belongs_to :post
end

底线是您需要确保您的 cmets 表中有 post_id 列。如果不这样做,您应该执行迁移以插入它:

#db/migrate/new_migration.rb
Class NewMigration
   def self.up
      add_column :comments, :post_id, :string
   end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多