0

我正在尝试向基本表单添加一些 ajax,尽管它一直呈现为 html

OpponentsController#create 作为 HTML 处理

我的观点

<%= form_for(@opponent, :remote => true, :url => { :controller => "opponents", :action => "create" }) do |f| %>
  <% if @opponent.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@opponent.errors.count, "error") %> prohibited this opponent from being saved:</h2>

      <ul>
      <% @opponent.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

控制器

# POST /opponents
  # POST /opponents.json
  def create
    @opponent = Opponent.new(params[:opponent])

    respond_to do |format|
      if @opponent.save
        format.html { redirect_to(opponents_url,
                                  :notice => "#{@opponent.name}  was successfully created.") }
        format.js { render }
      else
        format.html { render :action => "new" }
        format.json { render :json => @opponent.errors, :status => :unprocessable_entity }
      end
    end
  end

创建.js.erb

alert('Hello Rails');

enter code here
4

1 回答 1

1

AJAX 表单要求您加载 jquery 和 jquery_ujs。确保将它们包含在应用程序布局标题或 application.js 文件中,如下所示:

# application.js
//= require jquery
//= require jquery_ujs
于 2013-02-03T15:04:44.053 回答