rails通过ajax呈现视图

时间:2014-01-13 11:43:31

标签: javascript ruby-on-rails ajax forms

我有一个新表单,如果它没有通过验证,我想通过ajax呈现

在我的控制器中我有:

def create
  event = CEvent.new(params[:c_event])

  respond_to do |format|
    if event.save
      format.html { redirect_to admin_c_events_path }
    else
      format.js {redirect_to new_admin_c_event_path(:event => event.as_json, :errors => event.errors )}
    end
  end
end

表单有:remote => true参数,当我点击提交按钮时,表单通过js提交,但没有任何渲染,当我重新加载页面信息时,我需要渲染确定,然后我重新加载页面空的新表单渲染。

我的控制台输出:

Started GET "/admin/c_events/new?errors=%23%3CActiveModel%3A%3AErrors%3A0x007f74780d1d50%3E&event%5Babout%5D=&event%5Bbackground_content_type%5D=&event%5Bbackground_file_name%5D=&event%5Bbackground_file_size%5D=&event%5Bbackground_updated_at%5D=&event%5Bcontact_person_id%5D=11&event%5Bcreated_at%5D=&event%5Bfinish_at%5D=2014-01-13+11%3A19%3A00+UTC&event%5Bid%5D=&event%5Blocation%5D=&event%5Bname%5D=dewqr&event%5Bpress%5D=&event%5Bpress_contact_person_id%5D=11&event%5Bstart_at%5D=2014-01-13+11%3A19%3A00+UTC&event%5Bupdated_at%5D=&event%5Bvideo%5D=" for 127.0.0.1 at 2014-01-13 13:19:43 +0200
Processing by Admin::CEventsController#new as JS
  Parameters: {"errors"=>"#<ActiveModel::Errors:0x007f74780d1d50>", "event"=>{"about"=>"", "background_content_type"=>"", "background_file_name"=>"", "background_file_size"=>"", "background_updated_at"=>"", "contact_person_id"=>"11", "created_at"=>"", "finish_at"=>"2014-01-13 11:19:00 UTC", "id"=>"", "location"=>"", "name"=>"dewqr", "press"=>"", "press_contact_person_id"=>"11", "start_at"=>"2014-01-13 11:19:00 UTC", "updated_at"=>"", "video"=>""}}
  Person Load (1.2ms)  SELECT "people".* FROM "people" WHERE (p_type LIKE '%member%')
  Person Load (0.5ms)  SELECT "people".* FROM "people" WHERE (p_type LIKE '%mentor%')
  Rendered admin/c_events/_select_speakers.html.erb (4.0ms)
  Startup Load (0.4ms)  SELECT "startups".* FROM "startups" 
  Rendered admin/c_events/_select_startups.html.erb (4.6ms)
  Rendered admin/c_events/_schedule_fields.html.erb (13.8ms)
  CACHE (0.0ms)  SELECT "people".* FROM "people" WHERE (p_type LIKE '%member%')
  Rendered admin/c_events/new.html.erb within layouts/admin (116.0ms)
  Rendered layouts/_header.html.erb (3.3ms)
  Rendered admin/_top_bar.html.erb (4.8ms)
  Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 10216ms (Views: 180.0ms | ActiveRecord: 2.1ms)

在不重新加载页面的情况下渲染视图的方法是什么?

2 个答案:

答案 0 :(得分:1)

复制/粘贴准备好的代码,但这对我来说总是有效。

remote: true保留在表单中,使其成为ajax请求,并在创建操作时在控制器中添加format.js作为响应。

应用程序控制器

  ...
  after_filter :add_flash_to_header
  ...

  def add_flash_to_header
    # only run this in case it's an Ajax request.
    return unless request.xhr?

    # add different flashes to header
    response.headers['X-Flash-Error'] = flash[:error] unless flash[:error].blank?
    response.headers['X-Flash-Warning'] = flash[:warning] unless flash[:warning].blank?
    response.headers['X-Flash-Notice'] = flash[:notice] unless flash[:notice].blank?
    response.headers['X-Flash-Message'] = flash[:message] unless flash[:message].blank?

    # make sure flash does not appear on the next page
    flash.discard
  end
  ...

<强> create.js.erb

<% if @cevent.valid? %>
  $("#new_cevent")[0].reset();
  //something more you might need
<% else %>
  $(document).find(".new_cevent").prepend("<%= j(render 'shared/error_messages', object: @cevent) %>");
<% end %>

<强>视图/共享/ _error_messages.html.erb

<% if object.errors.any? %>
  <div class="alert alert-danger alert-block">
    <h2><%= pluralize(object.errors.count, "error") %> prohibited this schedule from being saved:</h2>
    <button type="button" class="close" data-dismiss="alert">x</button>
    <ul>
    <% object.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

答案 1 :(得分:1)

jsut替换为

format.js {render json: event}