2016-04-05 4 views
1

Ich benutze Rails 4 und haben vier Methoden an meinem application_controller.rb zur Einstellung Flash-Mitteilungen in RailsRails nicht Flash-Nachrichtenschlüssel nach Aktion zerstört

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 

Aber die flash Einstellung bei exclusion_notice_for verloren, nachdem der Umleitung bestehen bleiben die Aktion destroy. Die anderen Methoden funktionieren normal.

Der Regler

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 

Dies ist die Anwendung zur Zeit zerstören Taste genereted

Dies ist die application.rb Datei

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

Bitte senden Sie den Rest Ihres Controller-Code. –

+0

@PetrGazarov aktualisiert :) – rplaurindo

+0

@LucasNelson ich versuche, aber nicht funktioniert. – rplaurindo

Antwort

1

Flashing Methode werden alle Ihre Aktionen arbeiten einfach hinzufügen 'created' oder 'updated' String wie in der Destroy-Methode.

Sie haben Fehler Can't verify CSRF token authenticity

Sie diese Methode in application.rb Datei setzen kann, um alles zu reparieren.

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 

Kontroller

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

Fügen Sie einfach '' 'protected''' auf Methoden? – rplaurindo

+0

in 'application.rb' unter geschützter Leitung. – 7urkm3n

+0

Ich verstehe, aber es hat nicht funktioniert :( ** Obs. **: Leerzeichen zwischen Methodenname und '' '(' ' ' '' info_flahed ('' 'es ist ok,' '' info_fleshed ('' 'ein Fehler wird ausgelöst – rplaurindo

Verwandte Themen