Question

Question:

How to access hidden field post_id from file posts/show and use it in dashboard controller and index action ?.User creates comments in Dashboards/index view /action.

now, I am getting:

Couldn't find Post without an ID
app/controllers/dashboards_controller.rb:11:in `index'

What I already tried :

dashboards/index

  @comment_post = Post.find(params[:post][:post_id])
  -> undefined method `[]' for nil:NilClass

code

routes.rb

  resources :comments, :only => [ :create, :destroy ] 

view/dashboards/index

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

posts/show (view)

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

dashboards controller, index action:

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

generated 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>
Was it helpful?

Solution

If I understand correctly, do you execute the DashboardsController#index, show the page, and retrieve the hidden_field to DashboardsController#index again?

Rails executes the controller first, then render the view. It's a one way trip, never returns to controller again, except you click a button or what.

So you execute DashboardsController#index , get the enough information and transfer them to view/dashboards/index.

For this case, in controller, It seems like @feed_posts is enough.

class DashboardsController < ApplicationController
  def index   
    @feed_posts = blablablabla            
  end

end

posts/show, use post directly:

<%#= 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top