简体   繁体   中英

js.erb file not getting triggered

I have three files: Controller:

   def index
    @filterrific = initialize_filterrific(
      DimDictionary,
      params[:filterrific],
      select_options: {},
      persistence_id: 'shared_key',
      default_filter_params: {},
      available_filters: [ :dictionary_word_filter ]
    ) || return

    @dictionaries = @filterrific.find.limit(30)

    respond_to do |format|
      format.html
      format.js
    end
  end

Index.html.haml

= form_for_filterrific @filterrific do |f|
  .ui.form
    .fields
      .field
        Filter Word
        = f.text_field :dictionary_word_filter
  = render_filterrific_spinner
= render(partial: '/test/list', locals: { dictionaries: @dictionaries })

Index.js.erb

<% alert("hi"); %>
<% console.log('working Mannnnn') %>
<%  js = escape_javascript(render(partial: '/test/list', locals: { dictionaries: @dictionaries })) %>
$("#filterrific_results").html("<%= js %>");

The Index.js.erb is not getting triggered. Every time the page loads, its should run the Index.js.erb and show the alert and the console.log and the show the html right? Or the js.erb will only trigger on ajax request?

I am here using filterrific gem for filtration purpose but the index.js.erb file never gets triggered.

Even i have includes the js format in my controller. How should it be ? am i doing something wrong?

Every time the page loads, its should run the Index.js.erb and show the alert and the console.log and the show the html right?

No, format.js means, that your controller returns JS in response, when client wants to JS.

You should use remote:true for example, if you want to JS in responnse. There is no possibility to render both, because controller action can only repond to one HTTP request at a time.

More about responses in Rails: https://api.rubyonrails.org/classes/ActionController/MimeResponds.html

And about javascript in Rails: https://guides.rubyonrails.org/working_with_javascript_in_rails.html

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