Rails:如何使用嵌套属性表单在控制器操作中重定向



>我正在尝试在提交嵌套表单后重定向。

我有两个资源:productsproductvariants,其中producthas_manyproductvariants.在我的ProductsController.rb上,我有一个add_sku操作:

class Admin::ProductsController < ApplicationController
def add_sku
@product = Product.find(params[:id])
@product.productvariants.build
@page_title = 'Create new productvariant'
end

以及以下的相对形式:

<%= 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 %>

我想找出在我点击提交后如何重定向到productvariants的显示页面。类似于这样的东西:

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

我该如何实现这一点?

提前谢谢你!

根据我对你问题的上一条评论,您需要找到刚刚使用使 ProductVariant 记录唯一的参数哈希值创建的产品变型

@product_variant = ProductVariant.find_by(whatever params make this record unique)

然后

redirect_to @product_variant

最新更新