2017-05-29 5 views
0

Hallo Rails Community!Wie Sie Fehlermeldungen des zugehörigen Modells einrichten?

Ich habe booking_post Modell, das has_many Reservierungen hat.

class BookingPost < ApplicationRecord 
    has_many :reservations, dependent: :destroy 
end 

Alle Reservierung belongs_to booking_post und haben einige Validierungen

class Reservation < ApplicationRecord 
    belongs_to :booking_post 
    before_save { self.email = email.downcase } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, length: { maximum: 255 }, 
        format: { with: VALID_EMAIL_REGEX } 
    validates :name, :email, :phone_number, :start, :end, presence: true 
end 

Meine Routen sind weiter:

resources :booking_posts do 
    resources :reservations, only: [:new, :create] 
end 

Methoden:

class BookingPostsController < ApplicationController 
    def show 
    @booking_picture = @booking_post.booking_pictures.build 
    @booking_pictures = @booking_post.booking_pictures 
    @reservation = @booking_post.reservations.build 
    @reservations = @booking_post.reservations 
    end 
end 


class ReservationsController < ApplicationController 
    def new 
    @reservation = Reservation.new 
    end 
    def create 
    @booking_post = BookingPost.find(params[:booking_post_id]) 
    @email= User.where(admin: true).first.email 
    @reservation = @booking_post.reservations.build(reservation_params) 
     if @reservation.save 
     @saved_reservation = @reservation 
     redirect_to :back 
     flash[:notice] = 'Reservation was successfully created.' 
     ReservationMailer.fresh_message(@saved_reservation, @email).deliver_now 
     else 
     redirect_to @booking_post 
     flash[:info] = @reservation.errors.full_messages do |m| 
      m 
     end 
     end 
    end 
end 

ich auf booking_posts erstellen möchten /show.html.e rb form_for @reservation und rendert auf dieser Seite Fehler für @reservation. Wenn ich eine gültige @Reservation erstelle, sehe ich auf booking_posts/show.html.erb eine erfolgreiche Flash-Nachricht, aber ungültige @Reservation erscheinen ohne Fehler Flash-Nachrichten.

form_for @reservation auf booking_posts/show.html.erb:

<div class="card-action"> 
    <%= form_for([@reservation.booking_post, @reservation], html: {multipart: true}, class: "col s12") do |f| %> 
    <% if @reservation.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@reservation.errors.count, "error") %> prohibited this post from being saved:</h2> 

     <ul> 
     <% @reservation.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="col s6"> 
    <%= f.label :start %> 
    <%= f.date_field :start, placeholder: "start time", class: "datepicker" %> 
    </div> 
    <div class="col s6"> 
    <%= f.label :end %> 
    <%= f.date_field :end, placeholder: "end time", class: "datepicker" %> 
    </div> 
    <div class="col s6"> 
    <%= f.label :reservation_time %> 
    <%= f.time_field :reservation_time, placeholder: "time", class: "timepicker", id: "timepicker", type: "time" %> 
    </div> 

    <div class="input-field col s6"> 
    <%= f.label :name %> 
    <%= f.text_field :name, class: "validate" %> 
    </div> 
    <div class="input-field col s6"> 
    <%= f.label :email %> 
    <%= f.text_field :email, class: "validate" %> 
    </div> 
    <div class="input-field col s6"> 
    <%= f.label :phone_number %> 
    <%= f.text_field :phone_number, class: "validate" %> 
    </div> 

    <div class="waves-effect waves-light btn"> 
    <%= f.submit t(:submit_reservation)%> 
    </div> 

<% end %> 
<br> 
</div> 

Ich möchte Fehlermeldungen für @reservation auf @booking_post Seite (in booking_post_path, nicht in new_reservation_path oder anyting sonst) machen. Wie kann ich das tun?

Dank für Lösungen

Antwort

1

In Ihrem anderen Block, aktualisieren Sie es wie diese Junan

flash[:notice] = @reservation.errors.full_messages.to_sentence 
    redirect_to @booking_post 
+0

Dank. Es funktioniert. Ich habe eine kleine Frage. Ich habe versucht, Fehlerblitzfarbe zu ändern, indem ich flash [: danger], flash [: warning], flash [: alert] verwende und Meldungen nicht erscheinen. Ihre Lösung funktioniert nur mit Flash [: notice]. Mb du weißt warum. – anndrew78

+1

Um dies zu tun, müssen Sie die Rails-Flash-Schlüssel der Bootstrap-Alert-Klasse zuordnen. Bitte folgen Sie diesem sehr einfachen Tutorial. https://coderwall.com/p/jzofog/ruby-on-rails-flash-messages-with-bootstrap –

Verwandte Themen