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>

部分フィード投稿

<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 を実行し、十分な情報を取得して、view/dashboards/index に転送します。

この場合、コントローラでは @feed_posts で十分のようです。

class DashboardsController < ApplicationController
  def index   
    @feed_posts = blablablabla            
  end

end

posts/show、post を直接使用:

<%#= 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 に答える