Question

i am attempting to add some ajax to a basic form, though it keeps rendering as html

Processing by OpponentsController#create as HTML

my view

<%= 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 %>

controller

# 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

create.js.erb

alert('Hello Rails');

enter code here
Was it helpful?

Solution

AJAX forms require you to have jquery and jquery_ujs loaded. Make sure you include them in your application layout header or in your application.js file like so:

# application.js
//= require jquery
//= require jquery_ujs
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top