2016-04-05 1 views
1

я использую Rails 4, и имеют четыре метода на моем application_controller.rb для установки флэш-сообщений в RailsRails не сохраняются вспышки ключевое сообщение после того, как уничтожить действие

def exclusion_info_for model_name 
    flash[:notice] = "#{model_name.to_s.capitalize} has been deleted." 
end 

def creation_notice_for model_name 
    flash[:notice] = "#{model_name.to_s.capitalize} has been created." 
end 

def update_notice_for model_name 
    flash[:notice] = "#{model_name.to_s.capitalize} has been updated." 
end 

def error_notice 
    flash[:error] = "An unexpected error has it occurred" 
end 

Но установка flash на exclusion_notice_for теряется после перенаправления действие destroy. Другие методы работают нормально.

Контроллер

class CustomersController < ApplicationController 
    respond_to :html 

    def new 
    respond_with @customer = customer 
    end 

    def create 
    if @customer = Customer.create(customer_attrs) 
     creation_notice_for :customer 
    else 
     error_notice 
    end 
    respond_with @customer, location: "/" 
    end 

    def show 
    respond_with @customer = customer 
    end 

    def index 
    respond_with @customers = Customer.all 
    end 

    def edit 
    respond_with @customer = customer 
    end 

    def update 
    if @customer = customer.update(customer_attrs) 
     update_notice_for :customer 
    else 
     error_notice 
    end 
    respond_with @customer, location: "/" 
    end 

    def destroy 
    if @customer = customer.destroy() 
     exclusion_info_for :customer 
    else 
     error_notice 
    end 
    respond_with @customer, location: "/" 
    end 

    private 

    def customer 
    id ? Customer.find(id) : Customer.new 
    end 

    def customer_attrs 
    params.require(:customer).permit(:name) 
    end 

end 

В настоящее время эта кнопка уничтожить приложение genereted

Это application.rb файл

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :null_session 
    # protect_from_forgery with: :exception 

    include FormattedTime 

    def form_parent 
    ObjectSpace._id2ref(params[:form_parent_object_id].to_i) if params[:form_parent_object_id] 
    end 
    helper_method :form_parent 

    def root 
    render "layouts/application", layout: false 
    end 

    protected 

    def id 
    params[:id] 
    end 

    # refactored 
    def info_flashed model_name, action 
    flash[:notice] = "#{model_name.to_s.capitalize} has been #{action}." 
    end 

    def error_notice 
    flash[:error] = "An unexpected error has it occurred" 
    end 

end 
+0

Пожалуйста, пост остальную часть кода контроллера. –

+0

@PetrGazarov обновлен :) – rplaurindo

+0

@LucasNelson я стараюсь, но не работает. – rplaurindo

ответ

1

проблесковый метод будет работать все ваши действия просто добавить 'created' или 'updated' строка как в методе уничтожения.

Вы имели ошибку Can't verify CSRF token authenticity

Вы можете поместить этот метод в application.rb файл, чтобы исправить все это.

protect_from_forgery with: :exception, if: Proc.new { |c| c.request.format != 'application/json' } 
protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' } 

protected 

def info_flashed (model_name, action) 
    flash[:notice] = "#{model_name.to_s.capitalize} has been #{action}." 
end 

В контроллере

def destroy 
    if @customer = customer.destroy 
     info_flashed (@customer, 'deleted') 
    else 
     error_notice 
    end 
    respond_with @customer, location: "/" # you need to redirect correct path. 
    end 
+0

Просто добавьте '' 'protected''' методы? – rplaurindo

+0

в 'application.rb' под защищенной линией. – 7urkm3n

+0

Я понимаю, но это не сработало :( ** Obs. **: не вставлять пробелы между именем метода и '' '(' '' '' 'info_flashed (' '' это нормально, ' '' info_fleshed ('' 'ошибка поднята – rplaurindo

Смежные вопросы