Post的未知属性'category_id'

时间:2016-09-17 22:44:27

标签: ruby-on-rails

我正在尝试为我的帖子创建类别。为此,我生成了一个新的类别模型。虽然我已将两个模型关联起来,但我仍然收到此错误:

ActiveRecord::UnknownAttributeError: unknown attribute 'category_id' for Post.

我已经仔细检查了包含post表的category_id的架构:

add_index“comments”,[“post_id”],名称:“index_comments_on_post_id”

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.string   "cost"
    t.integer  "openspots"
    t.boolean  "approval"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
    t.integer  "category_id"
  end

  add_index "posts", ["user_id"], name: "index_posts_on_user_id"

以下是我的协会:

class Category < ActiveRecord::Base
  has_many :posts
end
class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :comments

  default_scope { order('created_at DESC') }
end

最后我想要包含我的帖子控制器:

class PostsController < ApplicationController
  def new
    # @post = Post.new
    @post = Post.new
    @categories = Category.all.map{|c| [ c.name, c.id ] }
  end

  def create
    @post = Post.new(params.require(:post).permit(:title, :description, :cost, :openspots, :approval, :category_id))
    @post.category_id = params[:category_id]
    @post.user = current_user
     if @post.save
       flash[:notice] = "Post was saved."
       redirect_to @post
     else
       flash[:error] = "There was an error saving the post. Please try again."
       render :new
     end
  end

  def update
    @post = Post.find(params[:id])
    @post.category_id = params[:category_id]
     if @post.update_attributes(params.require(:post).permit(:title, :description))
       flash[:notice] = "Post was updated."
       redirect_to @post
     else
       flash[:error] = "There was an error saving the post. Please try again."
       render :edit
     end
  end

  def edit
    @post = Post.find(params[:id])
    @post.category_id = params[:category_id]
  end

  def destroy
  end

  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
  end
end

每当我创建新帖子时,都会成功创建帖子;但是,类别ID永远不会保存。

以下是我的观点:

<h1>New Post</h1>

 <div class="row">
   <div class="col-md-4">
     <p>Guidelines for posts</p>
     <ul>
       <li>Make sure it rhymes.</li>
       <li>Don't use the letter "A".</li>
       <li>The incessant use of hashtags will get you banned.</li>
     </ul>
   </div>
   <div class="col-md-8">
     <%= form_for @post do |f| %>
       <div class="form-group">
         <%= f.label :title %>
         <%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
       </div>
       <div class="form-group">
         <%= f.label :description %>
         <%= f.text_area :description, rows: 8, class: 'form-control', placeholder: "Enter post description" %>
       </div>
       <div class="form-group">
         <%= f.label :cost %>
         <%= f.number_field :cost, rows: 8, class: 'form-control', placeholder: "Enter cost" %>
       </div>
       <div class="form-group">
         <%= f.label :openspots %>
         <%= f.number_field  :openspots, rows: 8, class: 'form-control', placeholder: "Enter Number of Open Spots" %>
       </div>
       <div class="form-group">
         <%= f.label :approval %>
         <%= f.check_box  :approval, rows: 8, class: 'form-control', placeholder: "Check if Approval Necessary" %>
       </div>
       <div class="form-group">
         <%= f.label :category %>
         <%= f.collection_select :category_id, Category.order(:name),:id,:name, include_blank: true %>
      </div>
       <div class="form-group">
         <%= f.submit "Save", class: 'btn btn-success' %>
       </div>
     <% end %>
   </div>
 </div>

0 个答案:

没有答案