Reputation: 2666
I am working on a shopping cart app and have a order model. I added shipping details to my order model to include carrier
, tracking_number
and ship_date
. I created a method called updateshipping
in order to have a different redirect after updating the order.
I'm new to rails so not sure if possible to have a conditional redirect in rails after save, or if I should create a custom route to access the updateshipping
method (what I tried), or if correct answer is that I should create a new model to hold the shipping details.
I tried to redirect the form partial to update via the method updateshipping
but the submit button doesn't work. When I hover over the submit button the url doesn't appear. When I hit Submit it appears to reload the form partial. If I remove everything after @order
in the form_for
it correctly goes to the update order/update method so I assuming my is issue is on the form_for
line (or that is part of the order model in the first place.)
<%= form_for(@order, :url => shipment_path, :html => { :method => "put" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :carrier %>
<%= f.text_field :carrier %>
<%= f.label :tracking_number, "Tracking #" %>
<%= f.text_field :tracking_number %>
<div class="form-actions">
<%= f.submit "Save changes", class: "btn btn-warning" %>
</div>
<% end %>
Orders Controller
def updateshipping
raise params.inspect (code to update the order will come later)
end
Rake Routes
shipment /shipmentdetails/:id(.:format) orders#shipment_details
shipment PUT /shipmentdetails/:id(.:format) orders#updateshipping
routes.rb
match '/shipmentdetails/:id' => 'orders#shipment_details', :as => :shipment
match '/shipmentdetails/:id' => 'orders#updateshipping', :as => :shipment, via: :put
Orders Model
class Order < ActiveRecord::Base
attr_accessible :name, :address, :carrier, :tracking_number, :ship_date
has_many :line_items, dependent: :destroy
has_one :user
has_one :shop
validates :name, :address, presence: true
end
Upvotes: 1
Views: 2532
Reputation: 2666
I'm not sure why this worked but I changed the route that linked to the form partial. I changed the route from:
match '/shipmentdetails/:id' => 'orders#shipment_details', :as => :shipment
to:
match '/shipmentdetails/:id/edit' => 'orders#shipment_details', :as => :edit_shipment
maybe it was getting confused between the shipment_path but I thought that the path from the form_for having the "put" method it should have known to use the following route:
match '/shipmentdetails/:id' => 'orders#updateshipping', :as => :shipment, via: :put
As for the form_for from my original attempt
form_for(@order, :url => shipment_path, :html => { :method => "put" })
it seems to work just like the the recommended
form_for(@order, :url => shipment_path(@order), :html => { :method => "put" })
but I'm not sure if it's a better practice to use shipment_path(@order) instead of just shipment_path.
Upvotes: 1
Reputation: 333
change the form_for line into :
form_for(@order, :url => shipment_path(@order), :html => { :method => "put" })
Upvotes: 0