如何显示before_action过滤器的flash消息authenticate_user!在通过ajax调用的动作上

时间:2016-07-28 14:07:09

标签: ruby-on-rails ajax

应用程序控制器

class ApplicationController < ActionController::Base

before_action :authenticate_user!, only: [:edit, :update, :destroy, :vote], not`enter code here`ice: "you must logged in first!!!" 
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) do |user_params|
      user_params.permit(:name, :email, :password,
        :password_confirmation, :avatar)
    end
    devise_parameter_sanitizer.permit(:account_update) do |user_params|
      user_params.permit(:name, :email, :password,
        :password_confirmation, :current_password, :remove_avatar, :avatar)
    end
  end
end

以下是ajax调用投票方法的post controller

帖子控制器

class PostsController < ApplicationController

  def index
    if params[:search]
      @posts = Post.search(params[:search]).order("created_at ASC")
    else
      @posts = Post.all.order('created_at ASC')
    end
  end

  def show_all_post_of_current_user
    @current_user_posts = current_user.posts.all
  end

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

  def vote
    @post = Post.find(params[:id])
    @vote = @post.votes.new :user_id => current_user.id, :vote => params[:vote]
    @vote.save
    respond_to do |format|
      format.html do
        if @vote.valid?
          flash[:notice] = 'Thanks for voting.'
        else
          flash[:error] = 'You can only vote on a post once.'
        end
        redirect_to :back
      end
      format.js
    end
  end  

  def new
    @post = current_user.posts.build
  end

  def display_post_of_current_user
    @display = current_user.posts.find(params[:id])
  end

  def edit
    @post = current_user.posts.find(params[:id])
  end

  def create
    @post = current_user.posts.build(post_params)
    if @post.save
      redirect_to display_post_path(@post)
    else
      render 'new'
    end
  end

  def update
    @post = current_user.posts.find(params[:id])
    if @post.update(edit_post_params)
      redirect_to display_post_path(@post)
      flash[:success] = "Post updated successfully"
    else
      render 'edit'
    end
  end

  def destroy
    @post = current_user.posts.find(params[:id])
    @post.destroy
    redirect_to post_path
    flash[:success] = "Post deleted successfully"
  end

  private
  def post_params
    params.require(:post).permit(:title, :description, :date, :category, :picture)
  end

  def edit_post_params
    params.require(:post).permit(:title, :description)
  end
end

查看

<h1>Post Description</h1>
<p>
Submitted <%= time_ago_in_words(@post.created_at) %> 
      by <%= @post.user.email %>

</p>
<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>
<p>
  <strong>Description:</strong>
  <%= @post.description %>
</p>

<p>
  <%= link_to vote_post_path(@post, vote: true), method: 'post', remote: true do %>
    <span> Up </span> | <span id='post_<%=@post.id%>_votes'><%= @post.up_votes %></span>
  <%end %>

  <%= link_to vote_post_path(@post, vote: false), method: 'post', remote: true do %>
    <span> Down </span> | <span id='post_<%=@post.id%>_votes'><%= @post.down_votes %></span>
  <%end %>

  <span id='post_<%=@post.id%>_votes'><%= @post.total_votes %> votes</span> |
  <%= link_to "Back", posts_path %> 
</p>

vote.js.erb

<% if @vote.valid? %>
  $("#post_<%= @post.id %>_votes").html("<%= @post.total_votes %> votes")
  $("#post_<%= @post.id %>up_votes").html("<%= @post.up_votes %>")
  $("#post_<%= @post.id %>down_votes").html("<%= @post.down_votes %>")
<% else %>
  alert('You can only vote on a post once.');
<% end %>

当用户点击upvote或downvote链接时,在post控制器的投票方法上调用ajax。应用程序控制器上的过滤器限制用户执行投票,直到我在过滤器之前使用login_in来验证用户的设计方法。 但是当用户点击upvote或downvote时,不知道如何生成闪存。

1 个答案:

答案 0 :(得分:2)

当用户点击投票时,您可以在js.erb文件中调用flash消息。

<强> vote.js.erb

<%= flash[:notice] %>

您可能需要更改您的respond_to块

respond_to do |format|
  if @vote.valid?
    format.html { flash[:notice] = 'Thanks for voting.' }
    format.js   { flash[:notice] = 'Thanks for voting.' }
  else
    format.html { flash[:error] = 'You can only vote on a post once.'}
    format.js   { flash[:error] = 'You can only vote on a post once.'}
  end
end

它也可以帮助你form_with_ajax

<强>更新 你需要自己做authenticate_user!动作,在此动作中,您可以拥有自己想要的行为。把消息放在js文件上你想要的东西

def authenticate_user!
  unless current_user
    render 'yourjs'
  end
end