传递参数以创建控制器轨道

时间:2015-04-29 05:29:38

标签: ruby-on-rails ruby-on-rails-4

我收到此错误:无法找到没有ID的博客。在我的参数中,我传递了一个blog_id所以我不确定我应该在这里做什么来允许创建评论

以下是我在错误页面上传递的参数:

     {"utf8"=>"✓",
     "authenticity_token"=>"b4+gKNVpEk2Bc2BBjZTiQ8CJchmOo6Bca4SU4e+2mHc1l8blRkeODrAbRw   xKZzI+FadXksgi5rtKbutXKfkSLw==",
     "comment"=>{"first_name"=>"Johnny",
     "last_name"=>"Manziel ",
      "content"=>"lkfewfewfkwekfwefioweifjiewf 4iwef ew",
      "blog_id"=>"6"},
      "commit"=>"Post"}

在评论控制器中:

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

观点:

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

1 个答案:

答案 0 :(得分:1)

您的blog_id位于评论内。因此,要访问它,您应该使用:

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