从 Rails 4 表单中的特定字段中提取信息

pulling information from specific fields in Rails 4 Form

我正在创建一个博客 Post 版本控制系统。 (我试过 Paper_trail 和 Draftsman,但他们没有我需要的东西)。当用户编辑 "live" 页面时,我的应用程序没有更改实时版本,而是使用新信息创建了一个 Post 版本 table 条目并将其称为 "pending"。但是,如果用户编辑 "draft" 页面,则不会创建 "pending" Post 版本,它只是直接编辑页面。

我似乎无法让表单参数从表单传递到 PostVersion.create!方法。当我试图从表单值中提取时,它只是提交了 nil 值。

posts_controller.rb

def update
  @post = Post.find(params[:id])
  if @post.status == 'draft'
   #this not important
  end
  if @post.status == 'live'
    @pending_post =  PostVersion.create!(title: params[:title], status:  'pending',  body: params[:body],  post_id:  @post.id )
  end
end

_form.html.slim

= simple_form_for [:admin, @post ], multipart: true do |f| 
  = f.error_notification 
  = f.input :title, label: 'Title', required: true, focus: true
  #rest of the form redacted

实际参数可能嵌套在"post" 排序数组下,例如post[title]。你可能想像这样定义一个特定的参数方法:

def update
  @post = Post.find(params[:id])
  if @post.status == 'draft'
   #this not important
  end
  if @post.status == 'live'
    @pending_post =  PostVersion.create!(title: post_params[:title], status:  'pending',  body: post_params[:body],  post_id:  @post.id )
  end
end

private

def post_params
  params.require(:post).permit :title, :body
end