Ruby on rails 简单的表格,注释don';I don’我没有如预期的那样出现

Ruby on rails 简单的表格,注释don';I don’我没有如预期的那样出现,ruby-on-rails,ruby-on-rails-4,simple-form,Ruby On Rails,Ruby On Rails 4,Simple Form,我对这些评论有意见。 如果我评论一个状态,不管它是什么,评论将显示在最后一个状态 希望你能帮忙,我可以编辑更多的代码,如果需要,让我知道你需要什么 \u form.html.erb <%= simple_form_for([status, status.comments.new]) do |f|%> <%= f.input :content %> <%= f.button :submit, "Comment", class:"btn btn-primary"%&

我对这些评论有意见。 如果我评论一个状态,不管它是什么,评论将显示在最后一个状态

希望你能帮忙,我可以编辑更多的代码,如果需要,让我知道你需要什么

\u form.html.erb

<%= simple_form_for([status, status.comments.new]) do |f|%>
 <%= f.input :content %>
 <%= f.button :submit, "Comment", class:"btn btn-primary"%>
<% end  %>
<% @statuses.each do |status| %>

  <%= image_tag status.user.avatar.thumb if status.user.avatar?%>
  <%= status.user.full_name%>
  <%= simple_format(status.content) %>
  <%= link_to time_ago_in_words(status.created_at) + " ago", status %>

  <% if status.user == current_user %>
    <span class="admin">
      <%= link_to "Edit", edit_status_path(status) %> |
      <%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure?"} %>
    </span>
  <% end  %>

  <% status.comments.each do |comment| %>
    <%= comment.content %>
  <% end %>

  <%= render partial: "comments/form", locals: {status: status} %>

<% end %>
Rails.application.routes.draw do

  get 'profiles/show'

  devise_for :users
  devise_scope :user do
    get 'register', to: 'devise/registrations#new', as: :register
    get 'login',    to: 'devise/sessions#new', as: :login
    get 'logout',   to: 'devise/sessions#destroy', as: :logout
  end
  resources :statuses do
    resources :comments
  end
  resources :comments

  get 'feed', to: "statuses#index", as: :feed
  root "statuses#index"

  get '/:id', to: "profiles#show"

end
class StatusesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
  before_action :set_status, only: [:show, :edit, :update, :destroy]

  def index
    @users = User.all
    @statuses = Status.all
    @comments = Comment.all
  end

  def show
    @status = Status.find(params[:id])
    @comments = @status.comments.all
  end

  def new
    @status = Status.new
    @comment = @status.comments.build
  end

  def create
    @status = Status.new(status_params)
    @status.user = current_user
    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render :show, status: :created, location: @status }
      else
        format.html { render :new }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @status.update(status_params)
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { render :show, status: :ok, location: @status }
      else
        format.html { render :edit }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @status.destroy
    respond_to do |format|
      format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_status
      @status = Status.find(params[:id])
    end

    def status_params
      params.require(:status).permit( :content)
    end
end
class CommentsController < ApplicationController

    def create
      @status = Status.find_by(params[:status_id])
      @comment = @status.comments.create(params_comment)
      @comment.save
      redirect_to statuses_path
    end

    def index
     @statuses = Status.all
     @comments = Comment.all
     @comment = Comment.find_by(params[:id])
    end


    def new
      @status  = Status.find(params[:status_id])
      @comment = Comment.new

    end
   private

   def params_comment
     params.require(:comment).permit(:content)
   end

end
状态\u控制器.rb

<%= simple_form_for([status, status.comments.new]) do |f|%>
 <%= f.input :content %>
 <%= f.button :submit, "Comment", class:"btn btn-primary"%>
<% end  %>
<% @statuses.each do |status| %>

  <%= image_tag status.user.avatar.thumb if status.user.avatar?%>
  <%= status.user.full_name%>
  <%= simple_format(status.content) %>
  <%= link_to time_ago_in_words(status.created_at) + " ago", status %>

  <% if status.user == current_user %>
    <span class="admin">
      <%= link_to "Edit", edit_status_path(status) %> |
      <%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure?"} %>
    </span>
  <% end  %>

  <% status.comments.each do |comment| %>
    <%= comment.content %>
  <% end %>

  <%= render partial: "comments/form", locals: {status: status} %>

<% end %>
Rails.application.routes.draw do

  get 'profiles/show'

  devise_for :users
  devise_scope :user do
    get 'register', to: 'devise/registrations#new', as: :register
    get 'login',    to: 'devise/sessions#new', as: :login
    get 'logout',   to: 'devise/sessions#destroy', as: :logout
  end
  resources :statuses do
    resources :comments
  end
  resources :comments

  get 'feed', to: "statuses#index", as: :feed
  root "statuses#index"

  get '/:id', to: "profiles#show"

end
class StatusesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
  before_action :set_status, only: [:show, :edit, :update, :destroy]

  def index
    @users = User.all
    @statuses = Status.all
    @comments = Comment.all
  end

  def show
    @status = Status.find(params[:id])
    @comments = @status.comments.all
  end

  def new
    @status = Status.new
    @comment = @status.comments.build
  end

  def create
    @status = Status.new(status_params)
    @status.user = current_user
    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render :show, status: :created, location: @status }
      else
        format.html { render :new }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @status.update(status_params)
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { render :show, status: :ok, location: @status }
      else
        format.html { render :edit }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @status.destroy
    respond_to do |format|
      format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_status
      @status = Status.find(params[:id])
    end

    def status_params
      params.require(:status).permit( :content)
    end
end
class CommentsController < ApplicationController

    def create
      @status = Status.find_by(params[:status_id])
      @comment = @status.comments.create(params_comment)
      @comment.save
      redirect_to statuses_path
    end

    def index
     @statuses = Status.all
     @comments = Comment.all
     @comment = Comment.find_by(params[:id])
    end


    def new
      @status  = Status.find(params[:status_id])
      @comment = Comment.new

    end
   private

   def params_comment
     params.require(:comment).permit(:content)
   end

end
class StatusesController
评论\u controller.rb

<%= simple_form_for([status, status.comments.new]) do |f|%>
 <%= f.input :content %>
 <%= f.button :submit, "Comment", class:"btn btn-primary"%>
<% end  %>
<% @statuses.each do |status| %>

  <%= image_tag status.user.avatar.thumb if status.user.avatar?%>
  <%= status.user.full_name%>
  <%= simple_format(status.content) %>
  <%= link_to time_ago_in_words(status.created_at) + " ago", status %>

  <% if status.user == current_user %>
    <span class="admin">
      <%= link_to "Edit", edit_status_path(status) %> |
      <%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure?"} %>
    </span>
  <% end  %>

  <% status.comments.each do |comment| %>
    <%= comment.content %>
  <% end %>

  <%= render partial: "comments/form", locals: {status: status} %>

<% end %>
Rails.application.routes.draw do

  get 'profiles/show'

  devise_for :users
  devise_scope :user do
    get 'register', to: 'devise/registrations#new', as: :register
    get 'login',    to: 'devise/sessions#new', as: :login
    get 'logout',   to: 'devise/sessions#destroy', as: :logout
  end
  resources :statuses do
    resources :comments
  end
  resources :comments

  get 'feed', to: "statuses#index", as: :feed
  root "statuses#index"

  get '/:id', to: "profiles#show"

end
class StatusesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
  before_action :set_status, only: [:show, :edit, :update, :destroy]

  def index
    @users = User.all
    @statuses = Status.all
    @comments = Comment.all
  end

  def show
    @status = Status.find(params[:id])
    @comments = @status.comments.all
  end

  def new
    @status = Status.new
    @comment = @status.comments.build
  end

  def create
    @status = Status.new(status_params)
    @status.user = current_user
    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render :show, status: :created, location: @status }
      else
        format.html { render :new }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @status.update(status_params)
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { render :show, status: :ok, location: @status }
      else
        format.html { render :edit }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @status.destroy
    respond_to do |format|
      format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_status
      @status = Status.find(params[:id])
    end

    def status_params
      params.require(:status).permit( :content)
    end
end
class CommentsController < ApplicationController

    def create
      @status = Status.find_by(params[:status_id])
      @comment = @status.comments.create(params_comment)
      @comment.save
      redirect_to statuses_path
    end

    def index
     @statuses = Status.all
     @comments = Comment.all
     @comment = Comment.find_by(params[:id])
    end


    def new
      @status  = Status.find(params[:status_id])
      @comment = Comment.new

    end
   private

   def params_comment
     params.require(:comment).permit(:content)
   end

end
class CommentsController
我的车型

class Comment < ActiveRecord::Base
  belongs_to :status
  belongs_to :user
end


class Status < ActiveRecord::Base
  belongs_to :user
  has_many  :comments

  default_scope -> { order(created_at: :DESC)}

  validates :content, presence: true,
            length: {minimum: 2}
  validates :user_id, presence: true
end

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  mount_uploader :avatar, AvatarUploader

  validates_presence_of   :avatar
  validates_integrity_of  :avatar
  validates_processing_of :avatar


  has_many :statuses
  has_many :users

  validates :first_name,    presence: true
  validates :last_name,     presence: true

  validates :profile_name,  presence: true,
                            uniqueness: true,
                            format: {
                                    with: /\A[a-zA-Z0-9_-]+\z/,

                                    message: "Must be formatted correctly." }

  def full_name
    first_name + " " + last_name
  end

  def gravatar_url
  stripped_email =  email.strip
  end
end
class注释{顺序(在::DESC创建)}
验证:内容,状态:真,
长度:{最小值:2}
验证:用户id,状态:true
结束
类用户
非常感谢。 这是我以自己的身份在这里被称为“耐莉·约翰”的时候…


这是我的评论,而不是我期望的地方。

你是否设置了一个属于
并且
在状态和评论之间有很多关系?我已经添加了我的模型,看起来它们还可以?你期望什么呢?我希望评论是在我评论的状态上,而不是在另一个状态上,这里的评论总是以最新状态出现…当我在rails控制台中创建状态评论时,我可以评论我想要的状态,并且在应用程序上。。。评论似乎没有重新确认状态id…你是否设置了一个
属于
并且
在状态和评论之间有很多
关系?我添加了我的模型,看起来它们还可以?你期望什么呢?我期望评论会出现在我评论的状态上,而不是另一个状态上,这里的评论总是以最新状态出现…当我在rails控制台中创建状态评论时,我可以评论我想要的状态,并且在应用程序上。。。似乎评论没有重新确认状态id。。。。