3

I am hoping you can help me figure out why I am getting this peculiar error.

I have the following form:

index.html.erb

<%= form_for :response, :url => {:action => 'create'}, :remote => true do |f| %> 
  <%= f.hidden_field :template_id, :value => @template.id %>
    <%= button_tag(type: 'submit', class: "btn btn-primary btn-lg pull-right next-slide") do %>
      Next &rarr;
     <% end %>
<% end %>

therapy_controller.erb

 def create
   @response = Response.new(response_params)
    respond_to do |format| 
      if @result = @response.save 
        format.html 
        format.js  
       else  
        format.html { render :action => "new" }  
        format.js
       end  
     end
   end

views/therapy/create.js.erb

<% if @result %>
  alert("Success!");
<% else %>
  alert("Fail!");
<% end %>

But, when I submit the form, I get the following error:

Template is missing:

Missing template therapy/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :slim]}. Searched in: *...

Any ideas why I would be getting this error? I can't figure out what's causing it.

Thanks!

UPDATE:

So, I've tried the suggestions by MrYoshiji, Dave, and Pavan, but I still get the same error. So, in my therapy_controller.erb, I have changed it to:

respond_to do |format|
  format.js
end

And now, I get the error:

ActionController::UnknownFormat in TherapyController#create 

Any ideas?

Update 2: Here are the server logs:

 Started POST "/therapy" for 152.79.45.121 at 2014-05-15 18:01:21 +0000
 Processing by TherapyController#create as HTML
  Parameters: {"utf8"=>"✓", "response"=>{"template_id"=>"2"}, "commit"=>"Save Response"}
 Completed 406 Not Acceptable in 1ms

  ActionController::UnknownFormat (ActionController::UnknownFormat):
    app/controllers/therapy_controller.rb:13:in `create'

And the routes.rb file is here: https://gist.github.com/dodinas/fb0a39282022e961ce09

Thanks.

Dodinas
  • 6,705
  • 22
  • 76
  • 108
  • ajax request should render js.erb not html.erb. it seems you are missing something – Dave May 09 '14 at 16:40
  • check if this link helps http://blog.markhorgan.com/?p=522 – Dave May 09 '14 at 16:48
  • Try with this `format.js { render 'create.js.erb' }` – Pavan May 09 '14 at 16:50
  • Did my answer worked for you? – Pavan May 12 '14 at 10:44
  • Hey Pavan, it did not work. See my update above, I get the ActionController Error. – Dodinas May 14 '14 at 16:38
  • @Dodinas Can you share the server logs generated upon form submission with your actual code(i.e., after removing change suggested by MrYoshiji, Dave, and Pavan). Add it in the question. Also, it would be great if you could share the relevant routes defined in `routes.rb`. – Kirti Thorat May 15 '14 at 17:09
  • @Dodinas - Change your index.html.erb form line to this: `form_for :response, :url => {:action => 'create', :format => :js}, :remote => true do |f|` and then try? Because, you need to request `create` method with a `:js` format to render the `create.js.erb` accordingly. – Surya May 15 '14 at 18:02
  • @Dodinas Do as Surya suggested. As per your server log, form is getting submitted with request as HTML not as JS. – Kirti Thorat May 15 '14 at 18:10
  • Thanks for the replies. I made the change as per Surya's suggestion. However, it now redirects the URL to "therapy.js" and it basically has text that says `alert("test");` – Dodinas May 15 '14 at 21:28
  • 1
    @Dodinas Redirects to "therapy.js"?? AFAIK, there's no redirect code you've mentioned here in question. You have to revert all changes made. Just change the "index.html.erb" from your question and keep everything as you've mentioned in your question. It should work!! – Surya May 16 '14 at 03:38
  • Ok, so I figured it out. Apparently, my "application.js" was not calling the jQuery UJS file. I don't know how this line got removed! SMH. Thanks for all your help. – Dodinas May 16 '14 at 15:10
  • Why didn't i get the awarded bounty? – Pavan May 23 '14 at 09:05

2 Answers2

5

Try with this

def create
  @response = Response.new(response_params)
  respond_to do |format| 
    if @result = @response.save 
      format.html 
      format.js { render 'create.js.erb' }
    else  
      format.html { render :action => "new" }  
      format.js
    end  
  end
end

Source

Community
  • 1
  • 1
Pavan
  • 33,316
  • 7
  • 50
  • 76
0

The error comes from the browser requesting HTML data and the controller only accepting js. I'd suggest adding :format => :js to form_for's options in index.html.erb:

<%= form_for :response, :url => {:action => 'create', :format => :js}, :remote => true do |f| %>

Source

mkis
  • 431
  • 2
  • 8