Reputation: 4962
When I submit the form below the "published" check_box value is not posting.
params3 = {"utf8"=>"✓", "authenticity_token"=>"i4SbblLJKIwba9yD30sDQCsir28/xdUxQZ90qYTNn0A=", "story"=>{"name"=>"asdsaddsad", "post"=>"asdasdasdasd", "user_id"=>"13", "image_id"=>"1", "published"=>"1"}, "commit"=>"Save Story", "action"=>"create", "controller"=>"stories"}
def create
@story = Story.new(story_params)
respond_to do |format|
if @story.save
format.html { redirect_to @story, notice: 'Story was successfully created.' }
format.json { render action: 'show', status: :created, location: @story }
else
format.html { render action: 'new' }
format.json { render json: @story.errors, status: :unprocessable_entity }
end
end
end
<%= form_for @story do |f| %>
<%= f.text_field :name, placeholder: "Enter Title" %>
<%= f.text_area :post, placeholder: "Enter Story" %>
<br/>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :image_id, value: @image.id %>
<%= f.label "Publish this" %>
<%= f.check_box :published %>
<%= f.submit "Save Story" %>
<% end %>
Upvotes: 0
Views: 31
Reputation: 494
The data that is being passed to the action is story
as seen in the params3
hash. Also, the published
check box is being posted. Check boxes by default will pass a 1
or 0
to denote true / false. Rails will update the value accordingly and accept 1
or 0
for the value of a checkbox:
params3 = {"utf8"=>"✓", "authenticity_token"=>"i4SbblLJKIwba9yD30sDQCsir28/xdUxQZ90qYTNn0A=", "**story**"=>{"name"=>"asdsaddsad", "post"=>"asdasdasdasd", "user_id"=>"13", "image_id"=>"1", "published"=>"1"}, "commit"=>"Save Story", "action"=>"create", "controller"=>"stories"}
Therefore your object creation will need to use those params. Since you are using Rails 4, you will need to use strong_parameters
which seems that you are. You will need to verify that published
is an allowed value in your params hash.
def story_params
params.require(:story).permit(...., :published, ...)
end
Upvotes: 1