我有一个带有以下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.facebook
是true
还是false
,这就是:facebook
为boolean
的原因。
然后,我创建了以下表单,以允许用户实际制作post.facebook
true
或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>
<%= 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
的默认值=&gt; false
:facebook
复选框的默认状态=&gt; unchecked
post.facebook
复选框时:facebook
的值=&gt; true
:facebook
复选框时:facebook
复选框的状态=&gt; checked
我发现了以下Stack Overflow问题,但无法从那里找出我的问题的解决方案:
这似乎是一个基本要求/问题,我相信我遵循了Rails文档,但我无法弄明白。
知道我错过了什么/做错了吗?
答案 0 :(得分:2)
你的facebook属性不在post参数中。
您必须做出的更改:
<%= form_for [@calendar, @post], html: { multipart: true } do |f| %>
和
<%= f.check_box :facebook, checked: true %>
答案 1 :(得分:2)
看起来你应该从@crispychicken
获得所需的信息,但是因为你似乎在努力让它发挥作用,这就是你需要做的事情:
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def new
@calendar = Calendar.find params[:calendar_id]
@post = @calendar.posts.new
end
def create
@calendar = Calendar.find params[:calendar_id]
@post = @calendar.posts.new post_params
@post.save
end
private
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
end
这将允许您在表单中使用以下内容:
#app/views/posts/new.html.erb
<%= form_for [@calendar, @post] do |f| %>
<%= f.check_box :facebook %>
<% end %>
这与您的其他代码一起,应该能够创建一个插入了post
值的新facebook
。
关于代码的一些指示:
-
<强>循环强>
始终使用循环或partials来删除效率低下的代码。
此外,请勿使用HTML来设置应用程序的样式。当您可以在其中一个div上轻松使用<hr>
时,您正在使用border-top
。 <hr>
应仅用于在您的实际内容中真正提供水平规则,不作为样式元素。
#app/views/shared/_errors.html.erb
<%= content_tag :div, class: error_explanation do %>
<%= content_tag :h2, "#{pluralize(@post.errors.count, "error")} prohibited this post from being saved:" %>
<%= content_tag :ul do %>
<%= errors.full_messages.each do |message| %>
<%= content_tag :li, message %>
<% end %>
<% end %>
<% end %>
#app/views/posts/new.html.erb
<%= form_for [@calendar, @post], html: { multipart: true } do |f| %>
<% render "errors", locals: { errors: @post.errors } if @post.errors.any? %>
<%= content_tag :div, class: "field" do %>
<%= content_tag :h3, "Post details" %>
<% time = %i(date time) %>
<% time.each do |t| %>
<%= content_tag :p do %>
<%= f.label :t %>
<%= f.send("#{t}_select", t %>
<% end %>
<% end %>
<%= content_tag :p do %>
<%= f.label :format %>
<%= f.select :format, ['Simple Status', 'Image', 'Link', 'Video'] %>
<% end %>
<%= content_tag :h3, "Post content" %>
<% opts = [[:subject, "text_field"],[:copy, "text_area"]] %>
<% opts.each do |type, field| %>
<%= content_tag :p do %>
<%= f.label type %>
<%= f.send(field, type) %>
<% end %>
<% end %>
<%= content_tag :p do %>
<%= f.label :short_copy, "Short copy (Twitter only)" %>
<%= f.text_area :short_copy %>
<% end%>
<% content_tag :h3, "Social channels" %>
<% social = %i(facebook twitter instagram pinterest google linkedin tumblr snapchat) %>
<% social.each do |s| %>
<%= content_tag :p do %>
<%= f.label_tag(s, s.to_s.titleize)%>
<%= f.check_box_tag s %>
<% end %>
<% end %>
<%= content_tag :h3, "Post Image" %>
<%= content_tag :p do %>
<%= f.label :image %>
<%= f.file_field :image %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>