Pregunta

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.

¿Fue útil?

Solución

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

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top