0

I want to call a specific action from a form_for and pass parameters.

I have a form_for that passes dates like so:

<%= form_for(:start_end_dates, url: url_for(controller: 'company_dashboard', action: 'shopify_orders_sync'), method: :get) do |f| %>
   <%= f.date_field :start_date, class: "form-control", required: true %>
   <%= f.date_field :end_date, class: "form-control", required: true %>
   <%= f.hidden_field :shop_id, value: @shop.id %>
   <%= f.submit "Apply" %>
<% end %>

But this attempt doesn't seem to be calling the method:

  def shopify_orders_sync
    @start_date = params[:start_date]
    @end_date = params[:end_date]
    @shop = Shop.find(params[:shop_id])
    if (params[:start_end_dates] && params[:start_end_dates][:start_date] && params[:start_end_dates][:end_date])
      start_date = params[:start_end_dates][:start_date].to_date.beginning_of_day
      end_date = params[:start_end_dates][:end_date].to_date.end_of_day
      if(start_date > end_date)
        ...
      else
        shop = Shop.find(params[:shop_id])
        session = ShopifyAPI::Session.new(domain: shop.shopify_domain, token: shop.shopify_token, api_version: '2019-04')
        ShopifyAPI::Base.activate_session(session)
        ...
        ...
        ...
        ...
        ... 
        ...
      end
    end
  end

Routes:

  get 'company/dashboard' => 'company_dashboard#dashboard'
  get 'company/shopify-orders-sync' => 'company_dashboard#shopify_orders_sync'

This form is in the #dashboard views.

If i do something like:

<%= form_for(:start_end_dates, url: company_orders_path, method: :get) do |f| %>
  <%= f.date_field :start_date, class: "form-control", required: true %>
  <%= f.date_field :end_date, class: "form-control", required: true %>
  <%= f.submit "Apply", class: "btn btn-primary" %>
<% end %>

Controller:

def orders
    ...
    if (params[:start_end_dates] && params[:start_end_dates][:start_date] && params[:start_end_dates][:end_date])
      start_date = params[:start_end_dates][:start_date].to_date.beginning_of_day
      end_date = params[:start_end_dates][:end_date].to_date.end_of_day
      if(start_date > end_date)
        redirect_to vendor_dashboard_path
        flash.keep[:notice] = "Dates are in wrong order."
      else
         ...
      end
      ...
    else
       ...
    end
    ...
  end

Routes:

get 'vendor/orders' => 'vendor_dashboard#orders'

This works, at least from the vendor/orders route. Haven't tried it elsewhere. The form is in the #orders and calls the #orders action.

What can I do to the form to keep everything working as is but then call the action/method def shopify_orders_sync ?

**

Attempt with:

**

<%= form_for :start_end_dates, url: '/company/shopify_orders_sync', method: 'get' do |f| %>

Result:

at=info method=GET path="/company/shop-orders?shop_id=31"
at=info method=GET path="/favicon.ico"
Started GET "/company/shop-orders?utf8=%E2%9C%93&start_end_dates%5Bstart_date%5D=2019-10-01&start_end_dates%5Bend_date%5D=2019-10-08&start_end_dates%5Bshop_id%5D=31&commit=Apply" for 69.118.200.110 at 2019-10-08 08:59:04 +0000
Processing by CompanyDashboardController#shop_orders as HTML
Parameters: {"utf8"=>"✓", "start_end_dates"=>{"start_date"=>"2019-10-01", "end_date"=>"2019-10-08", "shop_id"=>"31"}, "commit"=>"Apply"}
Rendering company_dashboard/shop_orders.html.erb within layouts/application
Rendered company_dashboard/shop_orders.html.erb within layouts/application (139.8ms)
Rendered layouts/_argon-navbar.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 161ms
at=info method=GET path="/company/shop-orders?utf8=%E2%9C%93&start_end_dates%5Bstart_date%5D=2019-10-01&start_end_dates%5Bend_date%5D=2019-10-08&start_end_dates%5Bshop_id%5D=31&commit=Apply" 
at=info method=GET path="/favicon.ico"

I have done many iterations of the above form_for but all with the same result.

uno
  • 1,421
  • 12
  • 38
  • Did you happen to look at this --> https://stackoverflow.com/questions/5320414/form-for-but-to-post-to-a-different-action – sureshprasanna70 Oct 08 '19 at 07:15
  • Yes I tried that and similar iterations. The `...(:start_end_dates...` may be interfering somehow. – uno Oct 08 '19 at 07:21
  • If this is a form for search functionality you may try `form_tag` instead of `form_for`. – sureshprasanna70 Oct 08 '19 at 07:23
  • What does the server log say about handling the request? It should specify incoming requests, the URL requested, incoming parameters and the controller that is responsible for handling that request. Could you add the server log for the submit request to the question? Or does the request never reach the server? Meaning the generated URL might be wrong. – 3limin4t0r Oct 08 '19 at 08:21
  • @3limin4t0r exactly, nothing is there. is everything I have set up enough to get this going? Currently, the way I got i working is to just use `<%= form_for(:start_end_dates, url: company_orders_path, method: :get) do |f| %>` (which is the method used for the view) and then inside the view to check the parameters and then call the method from there....I have tried so many ways to have this working from every stackoverflow post about this subject but none have seemed to be working – uno Oct 08 '19 at 08:46
  • @uno Could you then post the generated HTML for the form? – 3limin4t0r Oct 08 '19 at 08:51
  • Hey thanks... added it. @3limin4t0r ... Not sure what to do here. – uno Oct 08 '19 at 09:03
  • @uno You've currently posted the server log of the working request, which is not really what I need. In the browser navigate to the view that uses the form that isn't working and press `Ctrl + U` (for most browsers), then search your form and add it to the question. – 3limin4t0r Oct 08 '19 at 09:25

0 Answers0