I have a Rails 4 app with the following Post model:
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
Users must be allowed to decide whether post.facebook is true or false, which is the reason why :facebook is a boolean.
Then, I created the following form to allow users to actually make post.facebook true or false:
<%= 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>
<%= label_tag(:facebook, "Facebook" %>
<%= check_box_tag(:facebook) %>
</p>
<p>
<%= label_tag(:twitter, "Twitter") %>
<%= check_box_tag(:twitter) %>
</p>
<p>
<%= label_tag(:instagram, "Instagram") %>
<%= check_box_tag(:instagram) %>
</p>
<p>
<%= label_tag(:pinterest, "Pinterest") %>
<%= check_box_tag(:pinterest) %>
</p>
<p>
<%= label_tag(:google, "Google+") %>
<%= check_box_tag(:google) %>
</p>
<p>
<%= label_tag(:linkedin, "LinkedIn") %>
<%= check_box_tag(:linkedin) %>
</p>
<p>
<%= label_tag(:tumblr, "Tumblr") %>
<%= check_box_tag(:tumblr) %>
</p>
<p>
<%= label_tag(:snapchat, "Snapchat") %>
<%= 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 %>
And I did sanitize all parameters in my 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
The problem is, whenever I try to create a new post or edit an existing post, and check the :facebook checkbox, then save the post, the value is still set to nil.
How can I get the following behavior:
- Default value of
post.facebook=>false - Default state of the
:facebookcheckbox =>unchecked - Value of
post.facebookwhen the:facebookcheckbox is checked =>true - State of the
:facebookcheckbox when the:facebookcheckbox is checked =>checked
I found the following Stack Overflow questions but could not figure out a solution to my issue from there:
- Ruby on Rails - Checkbox not saving to database?
- Rails Checkbox not working - no error when submitting form
- Get checkbox to render as checked if boolean value is false
This seems like a basic requirement / problem, and I believe I followed the Rails documentation, but I can't figure it out.
Any idea of what I am missing / doing wrong?
Aucun commentaire:
Enregistrer un commentaire