如何在控制器索引动作中访问部分隐藏字段

时间:2013-08-14 08:17:59

标签: ruby-on-rails hidden-field partials

问题:

如何从文件posts/show访问隐藏字段post_id并在dashboard控制器和index操作中使用它?。用户在Dashboards / index view / action中创建注释。

现在,我得到了:

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

的routes.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>

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

1 个答案:

答案 0 :(得分:0)

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

Rails首先执行控制器,然后渲染视图。这是单行程,除了单击按钮或什么之外,再也不会返回控制器。

所以你执行DashboardsController #index,获取足够的信息并将它们传输到view / dashboards / 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>