添加评论时,页面不会刷新

时间:2016-05-17 08:03:02

标签: ruby-on-rails page-refresh

问题:当我创建属于帖子的评论时,即使通过重定向也不会刷新页面。我知道Ajax可以很好地处理这个问题,但我想知道它为什么不能刷新。当然,注释会很好地保存到注释表中,因此当我手动点击f5键时页面会刷新。

我使用的是Windows 10,Ruby 2.2.4和Rails 4.2.6。

enter image description here

模型/ bulletin.rb

class Bulletin < ActiveRecord::Base
  has_many :posts, dependent: :destroy
end

模型/ post.rb

class Post < ActiveRecord::Base
  belongs_to :bulletin
  mount_uploader :picture, PictureUploader
  has_many :comments, dependent: :destroy
end

模型/ comment.rb

class Comment < ActiveRecord::Base
  belongs_to :post
end

控制器/ bulletins_controller.rb

class BulletinsController < ApplicationController
  before_action :set_bulletin, only: [:show, :edit, :update, :destroy]

  def index
    @bulletins = Bulletin.all
  end

  def show
  end

  def new
    @bulletin = Bulletin.new
  end

  def edit
  end

  def create
    @bulletin = Bulletin.new(bulletin_params)

    respond_to do |format|
      if @bulletin.save
        format.html { redirect_to @bulletin, notice: 'Bulletin was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  def update
    respond_to do |format|
      if @bulletin.update(bulletin_params)
        format.html { redirect_to @bulletin, notice: 'Bulletin was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  def destroy
    @bulletin.destroy
    respond_to do |format|
      format.html { redirect_to bulletins_url, notice: 'Bulletin was successfully destroyed.' }
    end
  end

  private
    def set_bulletin
      @bulletin = Bulletin.find(params[:id])
    end

    def bulletin_params
      params.require(:bulletin).permit(:title, :description, :post_type)
    end
end

控制器/ posts_controller.rb

class PostsController < ApplicationController

  before_action :set_bulletin
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = @bulletin.posts.all
  end

  def show
  end

  def new
    @post = @bulletin.posts.new
  end

  def edit
  end

  def create
    @post = @bulletin.posts.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to [@post.bulletin, @post], notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

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

  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to bulletin_posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  def set_bulletin
    @bulletin = Bulletin.find(params[:bulletin_id])
  end

  def set_post
    @post = @bulletin.posts.find(params[:id])
  end

  def post_params
    params.require(:post).permit(:title, :content, :picture, :picture_cache)
  end
end

控制器/ comments_controller.rb

class CommentsController < ApplicationController
  before_action :set_post
  before_action :set_comment, only: :destroy

  def create
    @comment = @post.comments.new(comment_params)
    @comment.save

    redirect_to [@post.bulletin, @post]
  end

  def destroy
    @comment.destroy
  end

  private

  def set_post
    @post = Post.find(params[:post_id])
  end

  def set_comment
    @comment = @post.comments.find(params[:id])
  end

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

视图/帖/ show.html.erb

<h2><%= bulletin_name params[:bulletin_id] %></h2>

<table class='table'>
  <tr>
    <th>Title</th>
    <td><%= @post.title %></td>
  </tr>
  <tr>
    <th>Content</th>
    <td><%= @post.content %></td>
  </tr>
  <tr>
    <th>Created at</th>
    <td><%= @post.created_at %></td>
  </tr>
</table>

<%= link_to 'Edit', edit_bulletin_post_path(@post.bulletin, @post), class: 'btn btn-default' %>
<%= link_to 'Back', bulletin_posts_path, class: 'btn btn-default' %>

<%= render "comments/comments" %>

0 个答案:

没有答案