Actions_as_commentable examples?

I have a model that I want to be a comment on. It’s hard for me to create a form in the “show” view of the model that will allow me to create comments. I find no useful or useful examples. Can someone point me or show me an example of how to do this?

Example:

A simple blog application. I have a model called Mail. This is remarkable. Therefore, in the "show" view, I want to show the message, and at the bottom there are fields that, when completed and sent, create a new comment associated with this message and put it in the database.

It sounds simple, and it works for me, so I can display the comments that I sowed. I just can't get a job form to post new ones. Any help is appreciated.

+5
source share
3 answers

This is a very, very simple material, and you certainly need some better structure and approach to learning. Buying a book, such as Agile Web Development with Rails , is the only real way to find out, otherwise you will wander from problem to problem, really studying well.

Say you have a post that you want to comment on.

#routes.rb
map.resources :posts do |post|
  post.resources :comments
end

#post_controller.rb
def show
  @post.find params[:id]
  @comment = @post.comments.new
end

#posts/show.html.erb
<%- form_for [@post, @comment] do |f|-%>
  <%= f.text_area :body -%>
  <%= f.submit -%>
<%- end -%>

#comments_controller
def create
  @post = @post.find params[:post_id]
  @comment = @post.comments.new params[:comment]
  if @comment.save
    redirect_to @post
+3
source

Let's assume the Post model. Make sure you have

class Post < ActiveRecord::Base
acts_as_commentable
end

then in the view "Post # show"

  <%= form_tag "/posts/add_new_comment" do %>
    <%= hidden_field_tag "id", post.id %>
    <%= text_area_tag "comment[comment]" %>
    <%= submit_tag "Post Comment" %>
  <% end %>

and then in PostController

  def add_new_comment
    post = Post.find(params[:id])
    post.comments << Post.new(params[:comment])
    redirect_to :action => :show, :id => post
  end

and in routes.rb

  match "/posts/add_new_comment" => "posts#add_new_comment", :as => "add_new_comment_to_posts", :via => [:post]

Hope this gets up and running.

+27
source

, , README - . @Kunday. act_as_commentable ...

  • .
  • , .

, "", . gem, rails g comment, .

.

-, , , , gem README.

class Post < ActiveRecord::Base 
  acts_as_commentable
end

create. , :authenticate_user! , . current_user . , / .

class CommentsController < ApplicationController
  before_action :authenticate_user!

  def create
    post = Post.find_by(id: params[:id])
    comment = post.comments.build(comment_params)
    comment.user = current_user

    if comment.save
      flash[:notice] = "Comment has been created."
      redirect_to post
    else
      flash[:alert] = "Comment has not been created."
    end
  end

  private    
    def comment_params
      params.permit(:comment)
    end
end

. . , - , .

post 'comments' => 'comments#create', as: "create_comment"

as: "create_comment" , create_comment_path. , Post, . css.

<div class="comment-section">
  <%= form_tag create_comment_path, method: "post" do %>
    <%= hidden_field_tag "id", @post.id %>
      <%= text_area_tag :comment %>
      <%= submit_tag "Submit" %>
    <% end %>
</div>

, Post show.

css, comment.user.name , . email , .

<div class="comment_list">
  <% @comments.each do |comment| %>
    <%= comment.comment %> <br>
      <%= comment.user.name %> <br>
        <br>
    <% end %>
</div>

, , , @comments , Post , :

  def show
    @post = Post.find_by(id: params[:id])
    @comments = @post.comments.all
  end

. .

+1
source

All Articles