Search code examples
ruby-on-railsruby-on-rails-3routesujs

Rails 3: Remote (UJS) form being handled by index action instead of create


I'm making an app in Ruby (1.9.3-p0) on Rails (3.1.3) and I'm having trouble with a remote form that I'm using to create an object. The form is loaded onto a view by a remote link that puts it into a div (in the new action. The form is also labeled remote, but when I submit it, the POST request tries to be handled by the index action instead of the create action. Why does this happen? Shouldn't Rails automatically use the create action of the controller for the POST request on the base route? Here is the relevant code and messages:

Here is the remote link in the view which loads the form:

<div id="new_higher_education_study">
<%= link_to "Nuevo Estudio Superior", new_higher_education_study_path, :remote => true %>
</div>

Here is the controller that handles all these requests:

class HigherEducationStudiesController < ApplicationController

def new
    @higher_education_study = HigherEducationStudy.new

    respond_to do |format|
        format.js
    end
end

def create
    @study = HigherEducationStudy.new(params[:higher_education_study])
    @higher_education_studies = HigherEducationStudy.get_by_academic_background_id(UserSession.find.user.curriculum_vitae.academic_background_id)

    respond_to do |format|
        if @study.save
            flash[:notice] = "Se ha guardado el Estudio Superior exitosamente."
        else
            flash[:notice] = "Error en el guardado del Estudio Superior."
        end
        format.js
    end
end
end

Then, the respective views are:

new.js.erb:

$('#new_higher_education_study').html("<%= escape_javascript(render :partial => 'higher_education_studies/higher_education_study_form' ) %>");

create.js.erb:

$('#higher_education_studies_table').html("<%= escape_javascript( render :partial => 'higher_education_studies_table') %>")

In my routes.rb file, I only set Rails with the default resources for that controller like so: resources :higher_education_studies, :except => [:index]

The rake routes command confirms that the route POST '/higher_education_studies' is linked to the create action:

               higher_education_studies POST   /higher_education_studies(.:format)                    {:action=>"create", :controller=>"higher_education_studies"}
             new_higher_education_study GET    /higher_education_studies/new(.:format)                {:action=>"new", :controller=>"higher_education_studies"}
            edit_higher_education_study GET    /higher_education_studies/:id/edit(.:format)           {:action=>"edit", :controller=>"higher_education_studies"}
                 higher_education_study GET    /higher_education_studies/:id(.:format)                {:action=>"show", :controller=>"higher_education_studies"}
                                        PUT    /higher_education_studies/:id(.:format)                {:action=>"update", :controller=>"higher_education_studies"}
                                        DELETE /higher_education_studies/:id(.:format)                {:action=>"destroy", :controller=>"higher_education_studies"}

So I don't understand why when I submit the form, I get this in the server console:

Started POST "/higher_education_studies" for 127.0.0.1 at 2012-04-10 11:12:53 -0300
Processing by HigherEducationStudiesController#index as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"a7kFXPfLvYEBwXurV6rn7apwuAE5p0mGoD5vMaHdcCE=", "higher_education_study"=>{"institution_id"=>"1", "institution"=>"", "degree"=>"law", "major"=>"asdf", "years_studied"=>"6", "status"=>"incomplete"}, "date"=>{"year"=>"2012"}, "commit"=>"Guardar"}
Completed 500 Internal Server Error in 13ms

ActionView::MissingTemplate (Missing template higher_education_studies/index, application/index with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}. Searched in: ...

I tried putting the line post '/higher_education_studies', :to => 'higher_education_studies#create' expressly on the routes file, but that also didn't work. I'd thank you very much for any help on this problem.


Solution

  • In case someone has a similar problem in the future, it turned out another part of the routes.rb file was messing with the routing of this particular controller. Since many people are working on the project, maybe someone made a more general route that caught mine before the resources of my controller. I didn't look into which was the culprit for now, I just moved the resources :higher_education_studies to almost the top of the routes.rb file, and now it works.