Reputation: 4203
I have an Order model. Customers interact with the Order model through an Orders controller. Admins interact with the Order model through a Purchases controller.
Mostly it's working, except this happens:
What I really need to happen is this:
In app/controllers/purchases_controller.rb I have this:
def new
@purchase = Order.new
respond_with @purchase
end
If have tried variations like...
def new
@purchase = Order.new
respond_with @purchase, :controller => :purchases
end
...but nothing like that is documented for respond_with, and naturally it doesn't work. What can I do?
Upvotes: 5
Views: 3248
Reputation: 27374
The answer to this question is related to your last question. I've updated my answer there, but in a nutshell, the problem is not with respond_with
(which as @jiri-pospisil points out you don't really need) but with your form generated by simple_form_for
. The action url in that form defaults to /orders
because @purchase
is an instance of the class Order
.
To fix that problem, specify the url in the form:
= simple_form_for @purchase, :as => :purchase, :url => purchases_path(@purchase) do |f|
= f.error_notification
= f.input :name
= f.button :submit
You'll then find that you have another problem: after the new order (purchase) is created, respond_with
will redirect to the show
action of OrdersController
. To fix that, you can use the location
option:
def create
@purchase = Order.new(params[:purchase])
if @purchase.save
respond_with(@purchase, :location => purchases_path(@purchase))
...
As you can probably tell at this point, using two controllers for a single model this way becomes somewhat convoluted, so you might want to consider namespaces instead.
Upvotes: 2
Reputation: 14402
A few observations:
Upvotes: 3