Rails显示帖子类别

时间:2016-07-28 21:25:03

标签: ruby-on-rails ruby

我正在尝试将帖子类别分配到前端的新帖子。正在创建Post而没有错误,但是,查看rails控制台; postcategory_id显示为零。

知道为什么字段显示为零?

=> #<ActiveRecord::Relation [#<Post id: 85, content: "New post", postcover: nil, user_id: 1, created_at: "2016-07-28 20:46:46", updated_at: "2016-07-28 20:46:46", post_id: nil, postcategory_id: nil, postcategory_name: nil>]>

我能够生成postcategory_id的唯一方法是编辑并保存现有帖子。

形式:

.....<%= f.collection_select :postcategory_id, Postcategory.all, :id, :name, {prompt: "Select a category" }, input_html: { class: "control-label" } %>

创建动作:

def create
  @post = current_user.posts.build(post_params)

    @post.postcategory_id = params[:postcategory_id]

    respond_to do |f|
        if @post.save 
            f.html { redirect_to posts_path, notice: "Post created!" }
        else
            f.html { redirect_to posts_path, notice: "Error: Couldn't create post with no text." }
        end
    end
end

2 个答案:

答案 0 :(得分:0)

我强烈建议你使用byebug。在创建操作的第一行添加SELECT CASE WHEN DATEPART(yy, tblTaskEventsHistory.TimeIn) = 1900 THEN 'NO' ELSE 'YES' END AS Status; ,刷新页面,转到控制台以检查params的值[:postcategory_id]

答案 1 :(得分:0)

尝试:

= f.input :postcategory_id, :as => :select, :collection => Postcategory.all, :prompt => 'Select a category', input_html: { class: "control-label" }

并注释掉该行:

@post.postcategory_id = params[:postcategory_id]

关于创建动作。