Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9093629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:09:05+00:00 2026-06-16T23:09:05+00:00

rails 3 form partial <%= form_for(answer, :remote => true) do |f| %> <% if

  • 0

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") %> prohibited 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 %>

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

Can anyone tell me what I am doing wrong to make the javascript submit via ajax? When I use the submit button, the request is sent via ajax. If I use the onchange event for the radio button and attempt to submit the form via javascript, it thinks the request is HTML.

Any help would be much appreciated!

-J

EDIT:
So when I use the form submit button, the request is slightly different:

Processing by AnswersController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IcJkV1GnIOEGRs7kaRuVQsp0sTNtHQw0Q+HMM7m/mV0=", "answer"=>{"conduct_evaluation_id"=>"15", "question_id"=>"1", "program_block_id"=>"1", "answer"=>"true"}, "commit"=>"Answer"}

versus using the onchange javascript to trigger a rails.submit event:

Processing by AnswersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IcJkV1GnIOEGRs7kaRuVQsp0sTNtHQw0Q+HMM7m/mV0=", "answer"=>{"conduct_evaluation_id"=>"15", "question_id"=>"1", "program_block_id"=>"1", "answer"=>"true"}}

Does anyone know why this may be occurring? Do I need to specify additional parameters when triggering the submit.rails event? Please help! 😀

EDIT 2:
So I found a workaround. It works if I bind the change event for the radio buttons to actually clicking the submit button. This is not the ideal solution, but at least it works.

EDIT 3:
I have decided on the following coffeescript:

$(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 returned as the response and be automatically executed on success.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:09:06+00:00Added an answer on June 16, 2026 at 11:09 pm

    Using onchange() bypasses Rails magic so it won’t submit the form as you wish. Write your own change method, f.e.:

    $('input.radio_button_selector').change( function() {
        $.ajax({
            url: $('#my_form').attr('action'),
            data: $('#my_form').serialize()
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Rails how do I call a form from another model in any given
I have a quite average form in Rails, trough blog_kit <% form_for(@blog_post, :html =>
I have an ajax form for creating a new Category. <%= form_for(@category, :remote =>
I have a large Ruby on Rails form that has the following partial view
I have a form partial that looks like this: <%= form_for(@pool) do |f| %>
I have a Rails form, where I am trying to insert a Select formfield
In my rails form I'm using the select tag. <%= f.select :featured, Timeline::TIMELINE_FEATURED, :prompt
In a Ruby on Rails form, I'm trying to use a button instead of
I have a Rails form for email, and I would like to have auto-completion
In my rails form i am using a multi select tag code looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.