问题:
如何从文件中访问隐藏字段 post_id并在控制器和操作posts/show
中使用它?用户在仪表板/索引视图/操作中创建评论。dashboard
index
现在,我得到:
Couldn't find Post without an ID
app/controllers/dashboards_controller.rb:11:in `index'
我已经尝试过的:
仪表板/索引
@comment_post = Post.find(params[:post][:post_id])
-> undefined method `[]' for nil:NilClass
代码
路线.rb
resources :comments, :only => [ :create, :destroy ]
查看/仪表板/索引
<!--form for creating a new post-->
<section>
<%= render :template => 'posts/new' %>
</section>
<!--partial for post feeder -->
<section>
<%= render :partial => 'dashboards/feed_post' %>
</section>
</div>
</div>
部分 feed_post
<ul class="post-items">
<%if @feed_posts.any? %>
<% @feed_posts.each do |post| %>
<%= render :template => 'posts/show', :locals => {:post => post} %>
<% end %>
<% end %>
</ul>
帖子/节目(查看)
<li>
<span class="image"><%= image_tag post.image.url(:message) if post.image?%></span>
<span class="content"><%= post.text_html %></span>
<span class="tags">Tags:<%= post.tag_list %></span>
<span class="meta"> Posted <%= time_ago_in_words(post.created_at) %> ago.<%= post.user.full_name %></span>
</li>
<%= hidden_field_tag :post_id, post.id %>
# want to access this value in dashboards/index action
<div class="comments">
<p>this is a comment part - posts/show</p>
<%= render :partial => 'comments/form', :locals => { :comment => @new_comment, :post => post } %>
<%= render :partial => 'comments/comment', :locals => { :collection => @comments, :as => :comment, :post => post } %>
</div>
仪表板控制器,索引操作:
class DashboardsController < ApplicationController
def index
@comment_post = Post.find(params[:post_id])
# get the post_id, right now it is nil
# this is line no 11 in error message on start of this question ...
@comments = @comment_post.comment_threads.order('created_at desc')
@new_comment = Comment.build_from(@comment_post,currrent_user,'')
#my intended actions in comments/form, comments/comment ...
end
end
生成的html
<li>
<span class="image"></span>
<span class="content"><p>test <iframe width="520" height="274" src="//www.youtube.com/embed/qpvWrDh332U" frameborder="0" allowfullscreen></iframe></p></span>
<span class="tags">Tags:</span>
<span class="meta"> Posted about 18 hours ago.xxxx</span>
</li>
<input id="post_id" name="post_id" type="hidden" value="6" />
<div class="comments">
<p>this is a comment part</p>
<h4>this is comment form </h4>
<p>this is comment form </p>
<h4>Comment#comment</h4>
<p>this is comment#comment</p>
</div>