1

问题:

如何从文件中访问隐藏字段 post_id并在控制器和操作posts/show中使用它?用户在仪表板/索引视图/操作中创建评论。dashboardindex

现在,我得到:

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

1 回答 1

0

如果我理解正确,您是否执行 DashboardsController#index、显示页面并再次将 hidden_​​field 检索到 DashboardsController#index?

Rails 先执行控制器,然后渲染视图。这是单程旅行,永远不会再次返回控制器,除非您单击按钮或其他内容。

所以你执行 DashboardsController#index ,获取足够的信息并将它们传输到视图/仪表板/索引。

对于这种情况,在控制器中,@feed_posts 似乎就足够了。

class DashboardsController < ApplicationController
  def index   
    @feed_posts = blablablabla            
  end

end

帖子/节目,直接使用帖子:

<%#= 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    => Comment.build_from(post,currrent_user,''), :post => post  }  %>
  <%= render :partial => 'comments/comment', :locals => { :collection => post.comment_threads.order('created_at desc'), :as => :comment, :post => post } %>
</div>
于 2013-08-14T09:08:54.160 回答