Zippo9
Zippo9

Reputation: 706

Passing Parameters to Create Controller Rails

I am getting this error: Couldn't find Blog without an ID. In my parameters I am passing a blog_id so I am not sure what I should do here to allow a comment to be created

Here is my parameters being passed on the 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 do |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 Your First Name To Comment"  %>
    </div>

    <div class="col-sm-6">
    <%= f.text_field :last_name, class: 'form-control', :placeholder => "Please Add Your Last Name To Comment"%>
    </div>
    </div>
    <% end %>
    <br>
    <%= f.text_area :content, class: 'form-control', :placeholder => "Add your 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>

Upvotes: 0

Views: 80

Answers (1)

Abhi
Abhi

Reputation: 4261

Your blog_id is inside comment. So, to access it, you should use:

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

Upvotes: 1

Related Questions