简体   繁体   中英

Respond to js rendering as HTML Rails

I have problem with rendering partial from form_tag cuz when render js file, it is generated as HTML. I don't know what happening because I did it recently with modals and everything was fine. Have you any idea what I am doing wrong.

I have 2 files _file.html.erb and file.js.erb

file.js.erb

    $("#search_results").html("<%= escape_javascript(render 'search') %>");

View

    <%= form_tag(search_sample_path(format: :js) ,method: :post, remote: true, id: "search_script") do %>
     <%= text_field_tag :parameter, class: "form-control" %>
          <div class="row">
            <div class="col-md-12">
              <%= button_tag icon("fa", "search", "Search"), class: "btn btn-success" %>
            </div>
          </div>
    <% end %>

<div class="row">
  <div class="col-md-12">
    <div id="search_results">
    </div>
  </div>
</div>

Controller

def search
    if !(search_params[:sample].empty?)
      model = search_params[:method]&.sub("_", " ").titlecase.sub(" ", "")
      @method = model.constantize.joins(:sample).where(:samples => { sample_name: search_params[:sample] }).take
      unless search_params[:date_range].empty?
        date_start = search_params[:date_range][0, 10]
        date_end = search_params[:date_range][12, search_params[:data_range].length]
        @method = @method.where("Date(created_at) BETWEEN ? AND ?", date_start, date_end)
      end
      respond_to do |format|
        format.html
        format.js
      end
    else
      redirect_to samples_path
    end
  end

Search partial

<div class="card">
  <div class="card-body">
    <%= @method %>
  </div>
</div>

    Started GET "/samples.js?utf8=%E2%9C%93&sample=QC-PM%2F1&method=furnance&date_range=&button=" for 127.0.0.1 at 2019-08-05 13:19:03 +0200
Processing by SamplesController#index as JS
  Parameters: {"utf8"=>"✓", "sample"=>"QC-PM/1", "method"=>"furnance", "date_range"=>"", "button"=>""}
  User Load (1.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  ↳ app/controllers/application_controller.rb:7
Unpermitted parameters: :utf8, :button
Unpermitted parameters: :utf8, :button
Unpermitted parameters: :utf8, :button
  Furnance Load (0.9ms)  SELECT  `furnances`.* FROM `furnances` INNER JOIN `samples` ON `samples`.`id` = `furnances`.`sample_id` WHERE `samples`.`sample_name` = 'QC-PM/1' LIMIT 1
  ↳ app/controllers/samples_controller.rb:6
Unpermitted parameters: :utf8, :button
  Rendering samples/search_results.js.erb
  Rendered samples/_results.html.erb (0.4ms)
  Rendered samples/search_results.js.erb (20.1ms)
Completed 200 OK in 92ms (Views: 76.8ms | ActiveRecord: 1.9ms)

create search.js.erb

$("#search_results").html("<%= j(render(partial: 'search', locals: {method: @method})) %>");

Modify Search partial

<div class="card">
  <div class="card-body">
    <%= method %>
  </div>
</div>

I found solution for that, problem was with param. I used name for param which was using by Rails, (:method). I hope it will be helpful for someone.

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