lundi 30 novembre 2015

How can I display comments of microposts with Rails?

Can someone explain me how can I display the comments in User or Static_pages_controller and how can I write the _comments.html.erb for display the comments? Thank you so much for your attention..

comments_controller

def create
  @comment = Comment.new(comment_params.merge(micropost_id: params[:micropost_id], user: current_user))
  if @comment.save
     flash[:success] = "Comment created!"
     redirect_to current_user
 #......
  end

_comment_form

 <%= form_for([micropost, @comment]) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>
   <div class="field">
       <%= f.text_area :content %>
   </div>
   <button class="btn" type="submit">
        Create
   </button>
 <% end %>

static_pages_controller

def home
  if logged_in?
     @micropost  = current_user.microposts.build
     @feed_items = current_user.feed.paginate(page: params[:page])
     @comment = Comment.new
  end
end

_micropost.html.erb

 <%= render 'shared/comment_form', micropost: micropost %>

Aucun commentaire:

Enregistrer un commentaire