skip to Main Content

I have a form in index page and I want that the action will be executed by Ajax, to display the result in the Index page.
This is my form in the index Page:

        <%= form_with model: @shopping, :url => {:action => "searchByDates"}, remote: true do |form| %>
        <div class="form-group">
            <%= form.label :date_start %>
            <%= form.date_field :date_start, attribute: 'date' %>
            <%= form.label :date_end %>
            <%= form.date_field :date_end, attribute: 'date' %>
        </div>
        <div class="form-group">
            <%= form.submit "Cerca", class: 'btn btn-primary' %>
        </div>
    <% end %>

This is my controller action:

  def searchByDates
    @date_start = params[:date_start]
    @date_end = params[:date_end]
    @shoppings = Shopping.where(:date_shopping => @date_start..@date_end)
    respond_to do |format|
      format.html
      format.js
    end
  end

And this is my searchByDates.js.erb, in the folder of the view

$('#shoppingTable').html("<%= j render partial: 'shoppinglists' %>");

I have added the route in the routes.rb filepost 'shoppings/searchByDates'

What am I missing? Thanks

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution, I think that the problem was that it did not recognize the remote: true option, in fact it did always a redirect to the shoppings/search_by_dates URL, and that was the reason why it raised the "missing template" error, it searched for a view that I did not create, because I wanted the action to be called via Ajax.
    I changed remote: true, with local: false in the form:

    <%= form_with url: shoppings_search_by_dates_path, local: false do |form| %>
    

    This is the search_by_dates action

      def search_by_dates
        @shoppings = nil
        @date_start = params[:date_start]
        @date_end = params[:date_end]
        @shoppings = Shopping.where(:date_shopping => @date_start..@date_end).order(date_shopping: :asc)
        @result_total_price = @shoppings.sum(:total_price)
        respond_to do |format|
          format.html
          format.js
        end
      end
    

    And I renamed the js.erb file search_by_dates.js.erb


  2. you are missing the param format on your form.

        <%= form_with model: @shopping, url: { action: "searchByDates" }, format: :js, remote: true do |form| %>
            <div class="form-group">
                <%= form.label :date_start %>
                <%= form.date_field :date_start, attribute: 'date' %>
                <%= form.label :date_end %>
                <%= form.date_field :date_end, attribute: 'date' %>
            </div>
            <div class="form-group">
                <%= form.submit "Cerca", class: 'btn btn-primary' %>
            </div>
        <% end %>
    

    also I would modify the route to something like post "shoppings/searchByDates", to: shoppings/search_by_dates, as: shoppings_search_by_dates so you have snakecased actions while keeping the camelCase route, in that way the url param could be something like url: shoppings_search_by_dates_path

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search