javascriptjqueryruby-on-railsrubyrespond-to

respond_to do |format| gives "UnknownFormat in UsersController#show" in rails 5 app


In my rails 5 app I want to render partials in a div in the views/users/show.html.erb on link click.

I believe I have set things up accordingly but rails gives me this error ActionController::UnknownFormat in UsersController#show

ActionController::UnknownFormat

respond_to do |format|
    format.js { render :show_kwst }
end

I'm kind of lost here, I've done similar things in rail 4 by using bootstrap tabs with out any problems.

I've also added the 'responders' gem to the gemfile

This is my views/users/show.html.erb

<%= link_to 'Kwst', user_path%>

<div id="content"></div>

This is my users_controller.rb

 class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])

    @user_posts = @user.posts
    @user_kwsts = @user.kwsts


    respond_to do |format|
        format.js { render :show_kwst }
    end

  end

end

And here is the `views/users/show_kwst.js.erb``

$("#content").html("<%=escape_javascript(render :partial=>"shared/show_kwst")%>");

Am I missing something?


Solution

  • Try to the following order, you can follow this Rails Guide About format.js

    respond_to do |format|
       format.html { @user_kwsts }
       format.js
    end
    

    Notice the format.js in the respond_to block; that allows the controller to respond to your Ajax request. You then have a corresponding app/views/users/show_kwst.js.erb view file that generates the actual JavaScript code that will be sent and executed on the client side.