我的表格完美无缺:
def update
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
flash[:notice] = 'Blog post updated.'
redirect_to(:action => 'index')
else
render("edit")
end
end
但是如果用户在提交之前将表单留空,那么我想传入一个变量。否则,存储用户提交的值。我无法正确理解语法,我需要这样的东西:
def update
if params[:permalink] == nil
params[:permalink] = "#{defaultValue}"
end
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
flash[:notice] = 'Blog post updated.'
redirect_to(:action => 'index')
else
render("edit")
end
end
在表单中实际存在:permalink字段时,代码似乎没有抓到params[:permalink]
之前保存。有什么建议吗?
答案 0 :(得分:0)
解决方案:
def update
if !params[:post][:permalink].presence
params[:post][:permalink] = "#{defaultValue}"
end
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
flash[:notice] = 'Blog post updated.'
redirect_to(:action => 'index')
else
render("edit")
end
end