【发布时间】:2013-06-24 02:27:52
【问题描述】:
我想知道如何使用submit 按钮来更改布尔属性,而不是使用单选按钮。
例如,如果我在 Post#index 页面上显示“已发布”和未发布的文章帖子列表,并且我想要一个名为“发布”的按钮,它将 Post 模型上的 :is_published 字段设置为 true。
我在 Rails 3.2.13 上使用 strong_parameters
我在想我应该拥有的 Post 控制器
def index
@post = Post.recent
end
def update
@post = Post.find_by_slug(params[:id])
if params[:publish_button]
@post.is_published = true
@post.save
redirect_to root_path
end
end
private
def post_params
params.require(:person).permit(:body, :publish_button)
end
在我的Post#index 视图中,我有一个form_for,其中有<%= f.submit 'Publish', name: publish_button %>
这是Post#index中的视图
<%= form_for @post do |f| %>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
<%= f.submit_tag 'Publish Post', name: 'publish_post' %>
</div>
<% end %>
简单模型如下
class Post < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
scope :recent, -> { order('updated_at DESC') }
end
但我收到Required parameter missing: post 的错误
提前致谢。
更新 我添加了与问题相对应的模型和视图。希望对你有帮助。
【问题讨论】:
-
添加视图和模型会对您有所帮助。
-
您好 Wintermeyer,我已经添加了模型和视图。谢谢或您的意见。
标签: ruby-on-rails ruby-on-rails-3.2 simple-form form-for strong-parameters