创建评论(嵌套在路线中)的方法无效?

我的应用程序中有三种模型-用户,评论和帖子。用户可以有很多帖子。帖子属于用户。帖子可以有很多评论。评论属于用户和帖子。

我显示了来自用户控制器的帖子。我在路线的帖子资源中嵌套了评论。因为帖子属于帖子。

我不确定我是否应该在用户或帖子中嵌套评论。

路线:

 Rails.application.routes.draw do
  root 'mains#home'
  resources :users 
  resources :posts do 
    resources :reviews
  end
  resource :sessions,only: [:new,:create,:destroy]
  resources :passwords,:edit,:update]
end

用户模型:

class User < ApplicationRecord

  before_create { generate_token(:auth_token) }

  before_save { self.email = email.downcase }

  has_secure_password
  has_many :posts


  validates :name,presence: true
  validates :email,presence: true,uniqueness: true
  validates :password,confirmation: true
  validates :password_confirmation,unless: Proc.new { |a| !a.new_record? && 
  a.password.blank? }

  def send_password_reset
    generate_token(:reset_password_token)
    self.reset_password_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end


  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end

end

帖子的模型

class Post < ApplicationRecord
  belongs_to :user
  has_many :reviews,dependent: :destroy
  validates :title,presence: true
  validates :body,presence: true
end

评论模型

class Review < ApplicationRecord
    belongs_to :user
    belongs_to :post
end

用户控制器,显示方法:

  def show
    @user = User.find(params[:id])
    @posts = @user.posts
  end

Users#show-查看

<div class="row">
    <div class="col-md-12">   
        <h1> <%= @user.name %>  <%= @user.email %>  </h1>
    </div>
</div>

<div class="row">
  <div class="col-md-4">
     <%= link_to "New Post",new_post_path,class: "btn btn-primary btn-block" %>
   </div>
  <div class="col-md-8">
     <%= render 'posts/post' %>
  </div>
</div>

帖子/帖子

_post.html.erb

<h1> Posts ( <%= @posts.count %> ) </h1>
<% @posts.each do |p| %>
    <div class="form-field">

        <h4><b><%= p.title %></b>
            <%= link_to "Edit",edit_post_path(p.id),class: "btn btn-warning col-md-2- 
             offset",style: "align-right" %> 
            <%= link_to "Delete",p,method: :delete,class: "btn btn-danger col-md-2- 
        offset",data: { confirm: "You sure? "},style: "align-right" %> 
            Comments: <%= p.reviews.count  %> 

        <%= link_to "View Reviews" post_review_path(p.id),class: "btn btn- col-md-2-offset" 
      %>
   // I am able number of posts here but i am not able to display them
        </h4> 
        <%= p.body %>
    </div>
<% end %>

评论控制器

class ReviewsController < ApplicationController

  def show
    @post = Post.find(params[:id])
    @reviews = @post.reviews
  end

  def new
    @review = Review.new
  end

  def create
    @review = Review.new(comment_params)
    if @review.save
      flash[:success] = "Comment Posted succesfully"
      redirect_to user_path(current_user)
    end
  end

  private

  def comment_params
   params.require(:review).permit(:comment)
  end
end

评论#新-查看

<div class="row">
  <div class="col-md-offset-3 col-md-8">
   <b> Add a review </b>
  </div>
</div>

<%= form_for @review do |r| %>
  <div class="row">
    <div class="col-md-offset-3 col-md-7">
           <div class="form-group">
             <%= r.label :comment %>
             <%= r.text_area :comment,class: "form-control" %>
           </div>
            <%= r.submit "Add",class: "btn btn-primary btn-block" %>
    </div>
  </div>
 <% end %>






[![enter image description here][1]][1]


[1]: https://i.stack.imgur.com/EduSk.png
WAGXZ 回答:创建评论(嵌套在路线中)的方法无效?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2992225.html

大家都在问