HTMLとしてレンダリングし続けますが、基本的なフォームにajaxを追加しようとしています
OpponentsControllerによる処理#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
create.js.erb
alert('Hello Rails');
enter code here