0

我试图在模式窗口中使用 ajax 表单来允许我的用户编辑/创建新对象,我遇到的问题是错误没有被显示。

在我的控制器中,我有以下内容

 def create
    @user = User.find(current_user)
    @players = User.where(:team_id => current_user[:team_id]).all
    #params[:event][:datetime] = "#{params[:event][:date]} #{params[:event][:time]}"
    @event = Event.new(params[:event])
    respond_to do |format|
      if @event.save
        #Notifier.event_added(@event,@user).deliver
        format.html { redirect_to(events_url,
                                  :notice => "#{event_display_c(@event.event)} vs #{@event.opponent.name} was successfully created.") }
        format.js { @events = Event.all }
      else
        format.html { render :action => "new" }
        format.js { render :json => @event.errors, :status => :unprocessable_entity }
      end
    end
  end

在我的 create.js.erb 我有

$("#events_list").replaceWith("<%= escape_javascript render('events_list') %>");

在我的部分我有

 - if @event.errors.any?
  .alert-box.error
    %a.close{:href => ''} ×
    %strong Error:
    = pluralize(@event.errors.count, 'error')
    prohibited event from being saved:
    %ul
      - @event.errors.full_messages.each do |msg|
        %li= msg
%h5 Schedule Form
= form_for(@event, :html => { :class => "custom"}, :remote => true, :validate => true) do |f|
  .row
    .two.columns
      = f.label :event_type, :class =>'control-label'
    .four.columns
      = f.select :event, Event::EVENT_TYPES
    -if current_user.admin?
      .two.columns
        = f.label :team, :class =>'control-label'
      .four.columns
        = select("event", "team_id", Team.all.collect { |p| [truncate(p.name, :length => 18, :omission => '...'), p.id] }, {:prompt => 'Select a team'})
    - else
      =f.hidden_field :team_id, :value => current_user.team_id
  .row
    .two.columns
      = f.label :name, :class =>'control-label'
    .ten.columns
      = f.text_field :result_id, :class => "required"
  .row
    .two.columns
      = f.label :date_and_time, :class =>'control-label'
    .ten.columns
      .input-prepend
        %span.add-on
          %i.icon-schedule
      = f.text_field :time, :id => 'datepicker', :class => "hasDatepicker", "data-date-format" => "dd/mm/yyyy"
  .row
    .two.columns
      = f.label :duration, :class =>'control-label'
    .four.columns
      = f.number_field :duration, :placeholder => 'Time in Minutes'
    .two.columns
      = f.label :arrival_time, :class =>'control-label'
    .four.columns
      = f.number_field :arrival_time, :placeholder => 'Time in Minutes'
  .row
    .two.columns
      = f.label :location, :class =>'control-label'
    .four.columns
      = select("event", "location_id", Location.all.collect { |p| [p.name, p.id] }, {:include_blank => 'None'})
    .two.columns
      = f.label :opponent, :class =>'control-label'
    .four.columns
      = select("event", "opponent_id", Opponent.all.collect { |p| [p.name, p.id] }, {:include_blank => 'None'})
  .row
    .two.columns
      = f.label :players, :class =>'control-label'
    .four.columns
      = select(:event, :selected_players, @players.map { |p| [full_name(p), p.id] }, {:include_blank => false}, "data-placeholder" => 'Add Players to Lineup', :prompt => 'Add Players to Lineup', :multiple => "multiple", "data-customforms"=>"disabled")
    .two.columns
      = f.label :home_or_away, :class =>'control-label'
    .four.columns
      = f.select :home_or_away, Event::HOME_OR_AWAY, {:include_blank => 'None'}
  .row
    .two.columns
      = f.label :scores, :class =>'control-label'
    .three.columns
      = f.number_field :score_for, :placeholder => 'Our Score'
    .two.columns
      %label vs.
    .three.columns
      = f.number_field :score_against, :placeholder => 'Opponents Score'
  .row
    = f.submit :class => 'button success', :disable_with => "Saving Event..."
    = link_to 'Cancel', 'javascript:void(0);', :class => 'button alert close-reveal-modal'

控制台输出什么

   Started POST "/events" for 168.186.239.50 at 2013-02-08 06:59:47 +0100
Processing by EventsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"CBiKEQN+Q2Dkhgt7MSJbjU7WzuUPgW0/z6m71KdDpnU=", "event"=>{"event"=>"1", "team_id"=>"", "time"=>"", "duration"=>"", "arrival_time"=>"", "location_id"=>"", "opponent_id"=>"", "selected_players"=>[""], "home_or_away"=>"", "score_for"=>"", "score_against"=>""}, "commit"=>"Create Event"}
  User Load (1.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 38 LIMIT 1
  User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 38]]
  User Load (1.4ms)  SELECT "users".* FROM "users" WHERE "users"."team_id" = 1
   (0.4ms)  BEGIN
   (0.6ms)  ROLLBACK
  Event Load (0.9ms)  SELECT "events".* FROM "events" 
  Rendered events/_events_list.html.haml (1.1ms)
  Rendered events/create.js.erb (3.7ms)
Completed 200 OK in 516ms (Views: 13.3ms | ActiveRecord: 17.5ms)
4

1 回答 1

1

respond_to将块更改为

respond_to do |format|
  if @event.save
    #Notifier.event_added(@event,@user).deliver
    format.html { redirect_to(events_url,
                              :notice => "#{event_display_c(@event.event)} vs #{@event.opponent.name} was successfully created.") }
    format.js { @events = Event.all }
  else
    format.html { render :action => "new" }
    format.js
  end
end

并更改create.js.erb

`$("#events_list").html("<%= escape_javascript render('events_list') %>")`

因为你没有#events_list那个部分的 div

于 2013-02-08T03:32:56.497 回答