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.