2015-08-19 61 views
0

我正在创建博客帖子版本控制系统。 (我试过Paper_trail和Draftsman,他们没有我需要的东西)。当用户编辑“实时”页面时,我的应用程序会使用新信息制作PostVersion表格条目,并将其称为“挂起”,而不是更改实时版本。但是,如果用户编辑“草稿”页面,则不会创建“挂起”PostVersion,它只是直接编辑页面。

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

我似乎无法得到窗体参数从窗体传递到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 
+0

敢肯定你的形式params为内部嵌套的“后”,你可以随时检查它的服务器日志,如果是这样的话,那么你应该把它作为params [:post] [:title],等等。 –

回答

0

实际PARAMS可以在各种各样的 “后” 阵列被嵌套,如post[title]。你可能希望定义,像这样一个特定的PARAMS方法:

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 
+0

我得到这个错误“有一个错误:未定义的局部变量或方法'post_params'可能是因为我试图创建一个PostVersion使用来自用于编辑一个帖子的窗体中的信息(我确实有强大的参数设置) ...我尝试使用post_version_params,但也不起作用。 – NothingToSeeHere

+0

您可以编辑问题以包含您正在使用的强参数方法吗? –