2014-09-18 2 views
0

아래의 양식을 제출하면 "게시 됨"check_box 값이 게시되지 않습니다.check_box 값이 db에 게시되지 않는 이유

params3 = { "UTF8"=> "✓" "authenticity_token"=> "i4SbblLJKIwba9yD30sDQCsir28/xdUxQZ90qYTNn0A =" "이야기"=> { "이름"=> "asdsaddsad", "포스트" => "asdasdasdasd", "user_id"=> "13", "image_id"=> "1", "published"=> "1"} "commit"=> "Save Story", "action"= > "제어기"=> "층"}

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 %> 
+0

양식을 게시하고 '게시'데이터 유형을 알려주십시오. –

+0

죄송합니다. 중괄호로 감싸는 것을 잊지 마세요. – isea

답변

1

params3 해시 바와 같이 작용 story 인 전달되는 데이터를 "생성". 또한 published 확인란이 게시됩니다. 확인란의 기본값은 1 또는 0으로 true/false를 나타냅니다. 레일은 값을 적절하게 업데이트하고 체크 박스의 값을 1 또는 0을 받아 들일 것입니다 :

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"} 

따라서 개체 생성이 그 PARAMS를 사용해야합니다. Rails 4를 사용하기 때문에 strong_parameters을 사용해야합니다. params 해시에서 허용되는 값인 published인지 확인해야합니다.

def story_params 
    params.require(:story).permit(...., :published, ...) 
end 
관련 문제