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
Bitte senden Sie den Rest Ihres Controller-Code. –
@PetrGazarov aktualisiert :) – rplaurindo
@LucasNelson ich versuche, aber nicht funktioniert. – rplaurindo