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 8800629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:38:21+00:00 2026-06-14T00:38:21+00:00

I have a Rails controller set up that when a user creates a new

  • 0

I have a Rails controller set up that when a user creates a new record, it will redirect again to the new action and populate the form with new data, and this can continue on in a loop, creating a new record and getting redirected to the new action. What I am having trouble with is adding AJAX to this so that the user can stay in this loop without the page reloading. Here is my code without AJAX:

class ResponsesController
 def new
   @response = Response.new
   @answer_a = Answer.find(rand(1..100))
   @answer_b = Answer.find(rand(1..100))
   @answer_c = Answer.find(rand(1..100))
 end

 def create
   @response = current_user.responses.build(params[:response])
   if @response.save
      respond_to do |format|
        format.html { redirect_to new_response_path }
        format.js
      end
   else
      render 'new'
   end
 end
end

new.html.erb

 <div id="response_form">
   <%= render 'form' %>
 </div>

_form.html.erb

 <%= form_for @response do |f| %>   
    <%= f.radio_button :user_answer, @answer_a.id %> 
    <%= @answer_a.description %>
    <%= f.radio_button :user_answer, @answer_b.id %> 
    <%= @answer_b.description %>
    <%= f.radio_button :user_answer, @answer_c.id %> 
    <%= @answer_c.description %>
    <%= f.submit "Answer" %>                
 <% end %>

Below is my attempt to add AJAX. However, it is going to the show action and doing an update. How can I fix my code so that it keeps redirecting to the new action and the user can continue to create new records on an infinite loop?

 def create
   @answer_a = Answer.find(rand(1..100))
   @answer_b = Answer.find(rand(1..100))
   @answer_c = Answer.find(rand(1..100))
   @response = current_user.responses.build(params[:response])
   if @response.save
      respond_to do |format|
        format.html { redirect_to new_response_path }
        format.js
      end
   else
      render 'new'
   end
 end

_form.html.erb

 <%= form_for @response, :remote => true do |f| %>  
    <%= f.radio_button :user_answer, @answer_a.id %> 
    <%= @answer_a.description %>
    <%= f.radio_button :user_answer, @answer_b.id %> 
    <%= @answer_b.description %>
    <%= f.radio_button :user_answer, @answer_c.id %> 
    <%= @answer_c.description %>
    <%= f.submit "Answer" %>                
 <% end %>

create.js.erb

 $('#response_form').html('<%= j render("form") %>');
  • 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-14T00:38:22+00:00Added an answer on June 14, 2026 at 12:38 am

    The code seems fine by me. It shouldn’t be redirecting to the show action. Make sure that:

    1. You have gem 'jquery-rails' in your Gemfile

    2. You have <%= javascript_include_tag 'application' %> in your application.html.erb file

    3. You have the following lines in your application.js file

      //= require jquery
      //= require jquery_ujs
      

    Ok, so the problem here is that you are including the line <%= form_for @response do |f| %> inside the _form partial. Why is that a problem? Well, when an object @response does not exist, Rails renders the form like this:

    <form accept-charset="UTF-8" action="/responses" method="post">
    

    When an object @response does exist, Rails renders the form like this:

    <form accept-charset="UTF-8" action="/responses/id_of_the_response" method="post">
    

    Rails does this because it knows that @response exists so it’s only logical that you want to update it (and not create it again).

    The solution here is to change the form_for to this:

    <%= form_for @response, :url => { :action => :create, :method => :post } do |f| %>
    

    Also, be mindful that build is not saving the @response object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails controller action to test. In that action, a method User.can?
I am trying to create form that contains another model in rails. I have
I have an existing rails application to which i am adding a new controller
I have set up a rails application that uses single table inheritance but I
In my Rails app, when creating a business I have a form that has
I have a Rails 3 engine gem that is for basic user authentication and
I'm new to rails, and even more new to devise. I have set up
I have a Rails app set up using Devise, with a Registration controller. What's
In my Rails controller I have something like: def authenticate_user! if not current_user #
I have this command in a Rails controller open(source) { |s| content = s.read

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.