質問:post_id
ファイルから非表示のフィールド値にアクセスview/comments/_comment.html.erb
し、それを で使用するには どうすればよいcontrollers/dashboards_controller.rb
ですか? - 2 つのコントローラーがあります - ダッシュボードとコメント、および使用gem act_as_commentable_with_threading
今私は得る: ActiveRecord::RecordNotFound in DashboardsController#index Couldn't find Post without an ID
config/routes.rb
resources :comments, :only => [:create, :destroy]
コントローラー/dashboards_controller.rb
class DashboardsController < ApplicationController
def index
@post = Post.new
@user = current_user
@newest_users = User.newest_players
@feed_posts = Post.paginate(:page => params[:page], :per_page => 8)
@last_clubs = Club.last_clubs
@commented_post = Post.find(params[:post_id])
# trying to access params from view/comments/_comment.html.erb
# comments is another controller...
# do other operations with @commented_post
@comments = @commented_post.comment_threads.order('created_at desc')
@new_comment = Comment.build_from(@commented_post, current_user, '')
end
end
view/comments/_comment.html.erb コメントを追加
コメントフォームの場所
<div class="comment-form">
<%= form_for :comment, :remote => true do |f| %>
<%=f.hidden_field 'post_id', post.id %>
# need to use this value in dasboard controller
<%=f.text_field :body %>
<% end %>
</div>
ビュー/ダッシュボード/_feed_post.html.erb
<ul class="post-items">
<%if @feed_posts.any? %>
<% @feed_posts.each do |post| %>
<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>
<%= render 'comments/form' ,:locals => { :comment => @new_comment, :post_id => post.id } %>
<%= render 'comments/comment', :collection => @comments, :as => :comment, :post_id => post.id %>
</li>
<% end %>
<% end %>
</ul>
ビュー/ダッシュボード/インデックス
<div class="row">
<div class="span7">
<!--form for creating a new post-->
<section>
<%= render :template => 'posts/new' %>
</section>
<!--dashboard feed_post-->
<section>
<%= render :partial => 'dashboards/feed_post' %>
</section>
</div>