繁体   English   中英

Ruby on Rails 4.2.3 Flash通知/ redirect_to警报未在视图中显示

[英]Ruby on Rails 4.2.3 flash notice/alert on redirect_to not showing in view

值得注意的是,这是我的第一个Rails应用程序。 我已经阅读了很多,但是我尝试过的所有建议仍然没有用。 简而言之,我正在使用gem 'devise', '~> 3.5.1'和Rails Flash通知组件来通知用户,当他们试图在自己不拥有的模型中编辑记录时。 这是一个简单的“评论”应用程序,允许登录的用户提交评论,并且如果创建了评论,他们也可以编辑评论,但前提是必须创建评论。 这是到目前为止我得到的。

reviews_controller.rb

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

如果您在上述文件中的def edit下查看,则可以看到我已经尝试了一些方法来传递Flash通知。 无论尝试如何,都无法让它显示出来。 这是显示视图代码。

评论/ show.html.erb

<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.each循环中的指示传递消息和消息类型。 据我所知,这是在视图中显示Flash消息的非常标准的方法。

如果您需要,这是我的Gemfile

source 'https://rubygems.org'

ruby '2.1.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'devise', '~> 3.5.1'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

这里发生了什么? 我想念什么? 非常感谢!

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

<% flash.each do |name, msg| %>
  <%= content_tag(:div, msg, class: "alert alert-#{name}") %>
<% end %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM