Passing Parameters to Create Controller Rails -


i getting error: couldn't find blog without id. in parameters passing blog_id not sure should here allow comment created

here parameters being passed on error page:

     {"utf8"=>"✓",      "authenticity_token"=>"b4+gknvpek2bc2bbjztiq8cjchmoo6bca4su4e+2mhc1l8blrkeodrabrw   xkzzi+fadxksgi5rtkbutxkfkslw==",      "comment"=>{"first_name"=>"johnny",      "last_name"=>"manziel ",       "content"=>"lkfewfewfkwekfwefioweifjiewf 4iwef ew",       "blog_id"=>"6"},       "commit"=>"post"} 

in comment controller:

      def create       @blog = blog.find(params[:blog_id])       @comment = @post.comments.create!(comment_params)       redirect_to @blog       end 

in views:

    <ul class="media-list">   <li class="media">     <% comment = comment.new %>     <% comment.blog_id = @blog.id %>     <% if current_user.present?  %>     <% comment.user_id = current_user.id %>     <% end %>     <%= form_for comment |f| %>      <% if current_user.present? %>     <%= f.hidden_field :user_id, value = current_user.id %>     <% else %>     <div class="row">     <div class="col-sm-6">     <%= f.text_field :first_name, class: 'form-control',:placeholder => "please add first name comment"  %>     </div>      <div class="col-sm-6">     <%= f.text_field :last_name, class: 'form-control', :placeholder => "please add last name comment"%>     </div>     </div>     <% end %>     <br>     <%= f.text_area :content, class: 'form-control', :placeholder => "add comment", rows: 3 %>     <br>     <%= f.hidden_field :blog_id %>     <div class="row pull-right">       <div class="col-lg-6">       <%= f.submit "post", :class=> "btn-u btn-brd btn-brd-hover rounded-2x btn-u-blue" %>       </div>     </div>     <% end %>   </li> </ul> 

your blog_id inside comment. so, access it, should use:

@blog = blog.find(params[:comment][:blog_id]) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -