I am trying to redirect after a nested form has been submitted.
I have two resources: products
and productvariants
, where a product
has_many productvariants
. On my ProductsController.rb
I have an add_sku
action:
class Admin::ProductsController < ApplicationController
def add_sku
@product = Product.find(params[:id])
@product.productvariants.build
@page_title = 'Create new productvariant'
end
And a relative form of:
<%= form_for [:admin, @product], :html => {'role' => 'form' } do |f| %>
<%= f.fields_for :productvariants, Productvariant.new do |ff| %>
<div">
<%= ff.label :hero_id %>
<%= ff.select(:hero_id, Image.joins(:albums => :section).where(:sections => {:title => 'shop'}).order('file_name asc').map{|s| [s.file_name, s.id]}, {}, {}) %>
</div>
<div>
<%= ff.label "Variant title" %>
<%= ff.text_field :title %>
</div>
<div>
<%= ff.label :price %>
<%= ff.text_field :price %>
</div>
<div>
<%= ff.label :stock %>
<%= ff.text_field :stock %>
</div>
<% end %>
<div class="actions create">
<%= f.submit "Save" %>
</div>
<% end %>
I would like to find out how I would redirect to the show page of productvariants
after I hit submit. Something similar to this:
class Admin::ProductsController < ApplicationController
def add_sku
@product = Product.find(params[:id])
@product.productvariants.build
@page_title = 'Create new productvariant'
? if @product.productvariants.save
? flash[:notice] = "The productvariant has been successfully added to the product."
? redirect_to :controller => :productvariants, :action => :show, :id => @productvariant
? else
? render :action => :add_sku
? end
end
How could I implement this?
Thank you in advance!
As per my last comment on your question, you need to find the product variant that has just been created using the params hash values that make the ProductVariant record unique
@product_variant = ProductVariant.find_by(whatever params make this record unique)
then
redirect_to @product_variant