Need help with Rails 3 form partial
<%= form_for(answer, :remote => true) do |f| %>
<% if answer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(answer.errors.count, "error") %> prevented this answer from being saved:</h2>
<ul>
<% answer.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.hidden_field :conduct_evaluation_id, :value => conduct_evaluation.id %>
</div>
<div class="field">
<%= f.hidden_field :question_id, :value => question.id %>
</div>
<div class="field">
<%= f.hidden_field :program_block_id, :value => conduct_evaluation.program_block_id %>
</div>
<div class="field">
<%= f.radio_button :answer, true, :onchange => "$(this.form).trigger('submit.rails');" %>yes<br/>
<%= f.radio_button :answer, false, :onchange => "$(this.form).trigger('submit.rails');" %>no<br/>
</div>
<div class="actions">
<%= f.submit "Answer" %>
</div>
<% end %>
Issues with controller actions:
# POST /answers
# POST /answers.json
def create
@answer = Answer.new(params[:answer])
@answer.user = current_user
@answer.conduct_evaluation = ConductEvaluation.find(params[:answer][:conduct_evaluation_id])
respond_to do |format|
if @answer.save
format.js { }
format.html { redirect_to @answer, notice: 'Answer was successfully created.' }
format.json { render json: @answer, status: :created, location: @answer }
else
format.js { }
format.html { render action: "new" }
format.json { render json: @answer.errors, status: :unprocessable_entity }
end
end
end
# PUT /answers/1
# PUT /answers/1.json
def update
@answer = Answer.find(params[:id])
respond_to do |format|
if @answer.update_attributes(params[:answer])
format.js { }
format.html { redirect_to @answer, notice: 'Answer was successfully updated.' }
format.json { head :no_content }
else
format.js { }
format.html { render action: "edit" }
format.json { render json: @answer.errors, status: :unprocessable_entity }
end
end
end
Seeking JavaScript help for AJAX submission issue. The request is HTML instead of JS when using onchange event for radio button submit. Any input appreciated!
-J
Workaround discovered: Bind change event for radio buttons to clicking the submit button. Not ideal but functional.
CoffeeScript solution: $(document).ready -> $('#my_form input:submit').hide() $('#my_form input:radio').change -> $.ajax type: $(this.form).attr('method') url: $(this.form).attr('action') data: $(this.form).serialize() dataType: 'script' This allows the corresponding js action file to be automatically executed on success.