Ruby on Rails 4.2.3闪存通知/ redirect_to上的警告未显示在视图中

时间:2015-07-30 17:02:44

标签: ruby-on-rails flash devise

值得注意的是,这是我的第一个Rails应用程序。我已经对此做了相当多的阅读,但我尝试的所有建议仍然无效。简而言之,我正在使用using (MhEntities DContext = new MhEntities()) { string res = DContext.Database.SqlQuery<string>("SELECT MoneyforHealthEntities.Fn_LEVEL0_Acount_Id(@p0)", Account_Id).FirstOrDefault(); return Convert.ToInt64(res); } 和Rails flash通知组件来通知用户他们何时尝试编辑他们不拥有的模型中的记录。这是一个简单的评论应用程序,允许登录的用户提交评论,如果他们创建了评论,他们也可以编辑它,但前提是他们创建了评论。这是我到目前为止所做的。

reviews_controller.rb

gem 'devise', '~> 3.5.1'

如果您查看上述文件中的class ReviewsController < ApplicationController before_action :set_review, only: [:show, :edit, :update, :destroy] before_filter :authenticate_user!, except: [:index, :show] # GET /reviews # GET /reviews.json def index @reviews = Review.all end # GET /reviews/1 # GET /reviews/1.json def show end # GET /reviews/new def new #@review = Review.new @review = current_user.reviews.build end # GET /reviews/1/edit def edit @review = Review.find(params[:id]) if current_user.id == @review.user_id # default action else #flash[:alert] = "You do not have permission to edit this Review." #redirect_to @review #flash.alert = "You do not have permission to edit this Review." #redirect_to @review redirect_to @review, alert: 'You do not have permission to edit this Review.' #redirect_to @review, :flash => { :alert => 'You do not have permission to edit this Review.' } end end # POST /reviews # POST /reviews.json def create #@review = Review.new(review_params) @review = current_user.reviews.build(review_params) respond_to do |format| if @review.save format.html { redirect_to @review, notice: 'Review was successfully created.' } format.json { render :show, status: :created, location: @review } else format.html { render :new } format.json { render json: @review.errors, status: :unprocessable_entity } end end end # PATCH/PUT /reviews/1 # PATCH/PUT /reviews/1.json def update @review = Review.find(params[:id]) if current_user.id == @review.user_id respond_to do |format| if @review.update(review_params) format.html { redirect_to @review, notice: 'Review was successfully updated.' } format.json { render :show, status: :ok, location: @review } else format.html { render :edit } format.json { render json: @review.errors, status: :unprocessable_entity } end end else redirect_to @review, notice: 'You do not have permission to edit this Review.' end end # DELETE /reviews/1 # DELETE /reviews/1.json def destroy @review.destroy respond_to do |format| format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_review @review = Review.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def review_params params.require(:review).permit(:title, :content) end end ,您可以看到我尝试过一些事情来传递Flash通知。无论我尝试什么,我都无法让它出现。这是节目视图代码。

评论/ show.html.erb

def edit

它会在视图中显示<p id="notice"><%= notice %></p> <p id="alert"><%= alert %></p> <% flash.each do |name, msg| %> <% content_tag(:div, msg, class: "alert alert-#{name}") %> <% end %> <p> <strong>Title:</strong> <%= @review.title %> </p> <p> <strong>Content:</strong> <%= @review.content %> </p> <%= link_to 'Edit', edit_review_path(@review) %> | <%= link_to 'Back', reviews_path %> <p id="notice"><%= notice %></p>代码,但不会显示在<p id="alert"><%= alert %></p>循环内,这正是我要修复的内容。我想传递flash.each循环中指示的消息和消息类型。据我所知,这是在视图中显示Flash消息的非常标准的方式。

以下是我的 Gemfile ,以备不时之需。

flash.each

这里发生了什么?我错过了什么?非常感谢提前!

1 个答案:

答案 0 :(得分:1)

content_tag返回HTML标记,但不回显它。您需要自己回显204 No Content的返回值:

content_tag