Так что я пытаюсь создать функцию комментариев для своих сообщений в проекте rails, но имея возможность определить взгляды и контроллеры, чтобы быть в состоянии успешно сделать комментарий к сообщению. У меня есть то, что у меня есть ссылка на каждое сообщение в ленте новостей с комментариями, которые перенаправляют вас на страницу показа, на которой отображается один пост. на этой странице я хотел бы иметь форму, как и мою форму сообщения на домашней странице, но это было бы для комментариев на сообщениях, любой помощи, определяющей контроллеры, формы для комментариев и отображения их в столбце в после шоу страница была бы оцененаКомментарии к сообщениям в Rails
здесь шаблон контроллер и вид на сообщение контроллера
class PostsController < ApplicationController
before_action :logged_in_user, only: [:show, :create, :destroy]
before_action :correct_user, only: :destroy
def show
@post = Post.find(params[:id])
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
redirect_to request.referrer || root_url
end
private
def post_params
params.require(:post).permit(:content, :picture)
end
def correct_user
@post = current_user.posts.find_by(id: params[:id])
redirect_to root_url if @post.nil?
end
end
пост показать страницу
<% if logged_in? %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
</aside>
<div class="col-md-8 posts">
<h3>Comments</h3>
<%= render 'posts/post_show' %>
</div>
</div>
<% end %>
Мои комментарии контроллера до сих пор
class CommentsController < ApplicationController
before_action :loged_in_user, only:[:create, :destroy]
before_action :correct_user, only: :destroy
def create
@post = Post.find(params[:id])
@comment = Comment.new(params[:comment])
@comment.post = @post
@comment.user = current_user
if @comment.save
redirect_to post
else
render 'post/show'
end
end
def destroy
end
private
def comment_params
params.require(:comment).permit(:content, :picture)
end
def correct_user
@comment = current_user.comment.find_by(id: params[:id])
render 'post/show' if @comment.nil?
end
end
Как мой пост частичный визуализируется хотел бы, чтобы сделать комментарии, которые так же, как при пост в пост-шоу странице
<li id="post-<%= post.id %>">
<%= link_to gravatar_for(post.user), post.user %>
<span class="user"><%= link_to post.user.name, post.user %></span>
<span class="content"><%= post.content %></span>
<%= image_tag post.picture.url if post.picture? %>
<span class="timestamp">
Posted <%= time_ago_in_words(post.created_at) %> ago.
<%= link_to "comments", post %>
<% if current_user?(post.user) %>
<%= link_to "delete", post, method: :delete, data: { confirm: "You sure?" } %>
<% end %>
</span>
</li>
мой пост модели
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments, dependent: :destroy
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 500 }
validate :picture_size
private
def picture_size
if picture.size > 10.megabytes
errors.add(:picture, "should be less than 10MB")
end
end
end
и комментарии модели
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
default_scope -> { order(created_at)}
validates :user_id , pressence: true
validates :post_id , pressence: true
validates :content, presence: true, length: { maximum: 500 }
validate :picture_size
private
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5MB")
end
end
end
и это моя модель пользователя
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 250 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false}
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
def forget
update_attribute(:remember_digest, nil)
end
def activate
update_attribute(:activated, true)
update_attribute(:activated_at, Time.zone.now)
end
# Sends activation email.
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
# Sets the password reset attributes.
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
def feed
Post.where("user_id = ?", id)
end
private
def downcase_email
self.email = email.downcase
end
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
маршруты файл
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:show, :create, :destroy]
resources :comments, only: [:show, :create, :destroy]
end
Отправьте свой файл маршрута и треску для создания нового комментария. –
Эй, Арслан подвел мои маршруты, и код для создания новых комментариев находится в контроллере комментариев – Andre