ruby-on-rails - Rails 4 形式 : checkbox not saving value to database

标签 ruby-on-rails forms ruby-on-rails-4 checkbox persistence

我有一个带有以下 Post 的 Rails 4 应用程序型号:

create_table "posts", force: :cascade do |t|
    t.integer  "calendar_id"
    t.date     "date"
    t.time     "time"
    t.string   "subject"
    t.string   "format"
    t.text     "copy"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.string   "short_copy"
    t.integer  "score"
    t.boolean  "facebook"
    t.boolean  "twitter"
    t.boolean  "instagram"
    t.boolean  "pinterest"
    t.boolean  "google"
    t.boolean  "linkedin"
    t.boolean  "tumblr"
    t.boolean  "snapchat"
    t.string   "approval"
  end

必须允许用户决定是否post.facebooktruefalse ,这就是为什么 :facebook 的原因是 boolean .

然后,我创建了以下表格以允许用户实际制作 post.facebook truefalse :

<%= form_for [@calendar, @calendar.posts.build], html: { multipart: true } do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <hr>

  <div class="field">
    <h3>Post details</h3>
    <p>
      <%= f.label :date %>
      <%= f.date_select :date %>
    </p>
    <p>
      <%= f.label :time %>
      <%= f.time_select :time %>
    </p>
    <p>
      <%= f.label :format %>
      <%= f.select :format, ['Simple Status', 'Image', 'Link', 'Video'] %>
    </p>
    <hr>
    <h3>Post content</h3>
    <p>
      <%= f.label :subject %>
      <%= f.text_field :subject %>
    </p>
    <p>
      <%= f.label :copy %>
      <%= f.text_area :copy %>
    </p>
    <p>
      <%= f.label :short_copy, "Short copy (Twitter only)" %>
      <%= f.text_area :short_copy %>
    </p>
    <hr>
    <h3>Social channels</h3>
    <p>
      <%= f.label_tag(:facebook, "Facebook" )%>
      <%= f.check_box_tag(:facebook) %>
    </p>
    <p>
      <%= f.label_tag(:twitter, "Twitter") %>
      <%= f.check_box_tag(:twitter) %>
    </p>
    <p>
      <%= f.label_tag(:instagram, "Instagram") %>
      <%= f.check_box_tag(:instagram) %>
    </p>
    <p>
      <%= f.label_tag(:pinterest, "Pinterest") %>
      <%= f.check_box_tag(:pinterest) %>
    </p>
    <p>
      <%= f.label_tag(:google, "Google+") %>
      <%= f.check_box_tag(:google) %>
    </p>
    <p>
      <%= f.label_tag(:linkedin, "LinkedIn") %>
      <%= f.check_box_tag(:linkedin) %>
    </p>
    <p>
      <%= f.label_tag(:tumblr, "Tumblr") %>
      <%= check_box_tag(:tumblr) %>
    </p>
    <p>
      <%= f.label_tag(:snapchat, "Snapchat") %>
      <%= f.check_box_tag(:snapchat) %>
    </p>
    <hr>
    <h3>Image</h3>
    <p>
      <%= f.label :image %><br>
      <%= f.file_field :image %>
    </p>
  </div>
  <hr>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

而且我确实清理了我的 posts_controller.rb 中的所有参数:

def post_params
  params.require(:post).permit(:date, :time, :subject, :format, :copy, :image, :short_copy, :score, :facebook, :twitter, :instagram, :pinterest, :google, :linkedin, :tumblr, :snapchat, :approval)
end

问题是,每当我尝试 create新帖子或edit现有帖子,并检查 :facebook复选框,然后保存帖子,值仍然设置为 nil .

——————

更新:这是我尝试更新帖子时的服务器日志:

Started PATCH "/posts/2" for ::1 at 2015-10-16 11:13:38 -0700
Processing by PostsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"gqhmCHecVEJdqvOE18HQTEVk+jlcJ9pBi1hYJ+7GQuXHUW2VbDyZggDlCx9uKddI+iCXkd1yhrW9RWZXv4/oUw==", "post"=>{"date(1i)"=>"2015", "date(2i)"=>"9", "date(3i)"=>"22", "time(1i)"=>"2000", "time(2i)"=>"1", "time(3i)"=>"1", "time(4i)"=>"08", "time(5i)"=>"15", "format"=>"Image", "subject"=>"Yet another image test.", "copy"=>"What about it this time?", "short_copy"=>""}, "facebook"=>"1", "commit"=>"Update Post", "id"=>"2"}
  Post Load (0.1ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 2]]
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Redirected to http://localhost:3000/calendars/3
Completed 302 Found in 7ms (ActiveRecord: 0.3ms)

从那里,我会说 "facebook"=>"1"并且帖子已保存,但是当我转到 rails console 时在终端,我仍然得到 "facebook"=>nil .

——————

更新 2:这是我使用 <%= check_box_tag(["post"]["facebook"]) %> 时的新服务器日志:

Started PATCH "/posts/2" for ::1 at 2015-10-16 11:24:07 -0700
Processing by PostsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ByX04ciySTvKglgJGdTGsXSIOou443o02FfPJqNSn6BC3P980xKE+5fNoJKgPMG1y8xXIzm2JsDuSvFW8hs1Fg==", "post"=>{"date(1i)"=>"2015", "date(2i)"=>"9", "date(3i)"=>"22", "time(1i)"=>"2000", "time(2i)"=>"1", "time(3i)"=>"1", "time(4i)"=>"08", "time(5i)"=>"15", "format"=>"Image", "subject"=>"Yet another image test.", "copy"=>"What about it this time?", "short_copy"=>""}, "facebook"=>"1", "commit"=>"Update Post", "id"=>"2"}
  Post Load (0.1ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 2]]
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Redirected to http://localhost:3000/calendars/3
Completed 302 Found in 5ms (ActiveRecord: 0.3ms)

——————

更新 3:这里(再次)是我使用 <%= check_box_tag("post[facebook]") %> 时的一些新服务器日志:

Started PATCH "/posts/2" for ::1 at 2015-10-16 11:31:25 -0700
Processing by PostsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"vOBGP+yXhCd9GpIbOx6+UqZCptsQcBKvfX5MM2sgvHz5GU2i9zdJ5yBVaoCC9rlWGQbLc5ElTltLY3JDOmkWyg==", "post"=>{"date(1i)"=>"2015", "date(2i)"=>"9", "date(3i)"=>"22", "time(1i)"=>"2000", "time(2i)"=>"1", "time(3i)"=>"1", "time(4i)"=>"08", "time(5i)"=>"15", "format"=>"Image", "subject"=>"Yet another image test.", "copy"=>"What about it this time?", "short_copy"=>""}, "facebook"=>"1", "commit"=>"Update Post", "id"=>"2"}
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 2]]
   (0.2ms)  begin transaction
   (0.1ms)  commit transaction
Redirected to http://localhost:3000/calendars/3
Completed 302 Found in 6ms (ActiveRecord: 0.5ms)

——————

更新 4:还有一些服务器日志,当我使用 <%= f.checkbox :facebook %> 时:

Started PATCH "/posts/2" for ::1 at 2015-10-16 11:35:32 -0700
Processing by PostsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"kl0vM/bXVM+0m9z4JD4g/HcVA5YY4NYOfGnd/YP7kjjXpCSu7XeZD+nUJGOd1if4yFFuPpm1ivpKdOON0rI4jg==", "post"=>{"date(1i)"=>"2015", "date(2i)"=>"9", "date(3i)"=>"22", "time(1i)"=>"2000", "time(2i)"=>"1", "time(3i)"=>"1", "time(4i)"=>"08", "time(5i)"=>"15", "format"=>"Image", "subject"=>"Yet another image test.", "copy"=>"What about it this time?", "short_copy"=>""}, "facebook"=>"1", "commit"=>"Update Post", "id"=>"2"}
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", 2]]
   (0.2ms)  begin transaction
   (0.1ms)  commit transaction
Redirected to http://localhost:3000/calendars/3
Completed 302 Found in 9ms (ActiveRecord: 0.4ms)

——————

如何获得以下行为:

  • 默认值 post.facebook => false
  • :facebook 的默认状态复选框 => unchecked
  • post.facebook 的值当:facebook复选框已选中 => true
  • :facebook 的状态:facebook 时的复选框复选框已选中 => checked

我发现了以下 Stack Overflow 问题,但无法从那里找到解决我的问题的方法:

这似乎是一个基本要求/问题,我相信我遵循了 Rails 文档,但我无法弄明白。

知道我遗漏了什么/做错了什么吗?

最佳答案

您的 facebook 属性不在 post 参数内。

你必须做的改变:

<%= form_for [@calendar, @post], html: { multipart: true } do |f| %>

<%= f.check_box :facebook, checked: true %>

关于ruby-on-rails - Rails 4 形式 : checkbox not saving value to database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176924/

相关文章:

ruby-on-rails - 返回 : Collection select with with include_blank won't allow nil

ruby-on-rails - rails 语法错误 : wrong number of arguments

django - 如何处理 Django 验证错误

ajax - 如何在通过 Ajax 提交之前在表单上使用浏览器验证?

ruby-on-rails - sidekiq-superworker 的 superjob 完成时调用函数

ruby - 如何设置可以在 Rails 中的 Controller 和模型中访问的 "global"变量

ruby-on-rails - Rails authlogic : How to make Levels?

ruby-on-rails - iframe 的不同布局

javascript - 如何使用 JavaScript(js) 克隆和删除 html 表单?

ruby-on-rails - 无法使用 Rails 4 和 bootstrap-sass gem 在 Heroku 上使用 CSS