简体   繁体   中英

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

It's worth noting that this is my first Rails application. I have done quite a bit of reading on this but all the suggestions I've tried still do not work. In short, I'm using gem 'devise', '~> 3.5.1' and the Rails flash notification component to notify a user when they are trying to edit a record in a model that they do not own. It is a simple Reviews application, allowing users that are logged in to submit a review, and if they created it, they can edit it, as well, but only if they created it. Here's what I've got so far.

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

if you look under def edit in the above file, you can see I've tried a few things to pass the flash notification. I cannot get it to appear no matter what I try. Here's the show view code.

reviews/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 %>

It will show up in the view with <p id="notice"><%= notice %></p> or the <p id="alert"><%= alert %></p> code but not within the flash.each loop, which is what I'm trying to fix. I'd like to pass the message and message-type as indicated in the flash.each loop. As far as I can tell, this is pretty standard way of displaying flash messages in a view.

Here is my Gemfile in case you need it.

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

What's going on here? What am I missing? Thanks so much in advance!

content_tag returns an HTML tag but does not echo it. You need to echo the return value of content_tag yourself:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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