简体   繁体   中英

ActionController::UnknownFormat in CareersController#create for rails 4

i am trying to intigrate js file for "success message" as pop up when I submit my form. its says, ActionController::UnknownFormat in CareersController#create

In my controller:

   def create
    @career = Career.new(career_params)

    respond_to do |format|
      if @career.save
        format.js {render :template => 'careers/create', :locals => { :career => @career}  }
      else
        format.html { render :new, :locals => { :career => @career} }
      end
    end
  end

and my create.js.erb file:

$(document).ready(function(){

    $("#new_career").submit(function(e){
        var postData = $(this).serializeArray();
        var formURL = $(this).attr("action");
        $.ajax(
                {
                    url : formURL,
                    type: "POST",
                    data : postData,
                    format: 'js',
        success:function(data, textStatus, jqXHR)
        {
            alert("Form has been submitted");
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert("Network connection problem");
        }
    });
    e.preventDefault(); //STOP default action
    e.unbind(); //unbind. to stop multiple form submit.
});

})

Here is my server Error log :

Started POST "/careers" for ::1 at 2015-06-15 21:54:32 +0600
Processing by CareersController#create as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"3NOJnGQZsFuYtd4JdNGl4wVmIh7at3laQDfjYyp1iPxt/xUdokGqSaAQiWOb+zEh2yvrW6IE3CrnPXQhwBADTg==", "career"=>{"full_name"=>"mezbah", "email
"=>"mezbahalam26@gmail.com", "phone_number"=>"01742626262"}, "commit"=>"SUBMIT APPLICATION"}
   (0.0ms)  begin transaction
  SQL (1.0ms)  INSERT INTO "careers" ("full_name", "email", "phone_number", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["full_name", "mezbah"], ["email", "mezbahalam26@gmai
l.com"], ["phone_number", "01742626262"], ["created_at", "2015-06-15 15:54:32.107023"], ["updated_at", "2015-06-15 15:54:32.107023"]]
   (172.2ms)  commit transaction
Completed 406 Not Acceptable in 178ms (ActiveRecord: 173.2ms)

ActionController::UnknownFormat (ActionController::UnknownFormat):
  app/controllers/careers_controller.rb:21:in `create'


  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (48.0ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0x42c5208 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<
Mime::Type:0x42c4458 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x4267e48 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]

what am i doing wrong ? please help

ActionController::UnknownFormat in CareersController#create

You have defined format as json instead js . Change it to like this

def create
    @career = Career.new(career_params)

    respond_to do |format|
      if @career.save
        format.js {render :template => 'careers/create', :locals => { :career => @career}  }
      else
        format.html { render :new, :locals => { :career => @career} }
      end
    end
  end

Update

You should also have to define format as js in your ajax

#create.js.erb
$(document).ready(function(){

    $("#new_career").submit(function(e){
        var postData = $(this).serializeArray();
        var formURL = $(this).attr("action");
        $.ajax(
            {
                url : formURL,
                type: "POST",
                data : postData,
                format: 'js', #here
                success:function(data, textStatus, jqXHR)
                {
                    alert("Form has been submitted");
                },
                error: function(jqXHR, textStatus, errorThrown){
                    alert("Network connection problem");
                }
            });
        e.preventDefault(); //STOP default action
        e.unbind(); //unbind. to stop multiple form submit.
    });

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM