On Rails 4.我正在开发一个电子商务应用程序,用户可以选择通过类似向导的过程。
在数据库中没有创建或更新行,我只希望结果显示在第四页上(因为此信息随后被提供给第三方服务)。
因为数组不会超过五个或六个选择,所以我通过URL参数传递产品ID。这是控制器(让我们说第二页用户选择纸制品,第三页用户选择铅笔产品)
class WizardController < ApplicationController
def index
# Where the user clicks on a color scheme link
end
def paper
@color = ColorScheme.find(params[:color_scheme_id])
@product = Product.all
end
def paper_submit
respond_to do |format|
format.html { redirect_to wizard_pencils_path(
color_scheme_id: params[:color_scheme][:color_scheme_id],
id: params[:ids]) }
end
end
def pencils
@color = ColorScheme.find(params[:color_scheme_id])
@product = Product.all
@paper = Product.find(params[:id])
end
def pencils_submit
respond_to do |format|
format.html { redirect_to wizard_finish_path(
color_scheme_id: params[:color_scheme][:color_scheme_id],
id: params[:ids] + [:paper_ids]) } # ?? Not sure what goes here
end
end
def finish
# Haven't gotten here yet
end
private
def color_scheme_params
params.require(:wizard).permit(:color_scheme_id, :id)
# Also not too sure about this area either
end
end
路线:
get '/wizard/paper', controller: 'wizard', action: 'paper'
put '/wizard/paper', controller: 'wizard', action: 'paper_submit'
get '/wizard/pencils', controller: 'wizard', action: 'pencils'
put 'wizard/pencils', controller: 'wizard', action: 'pencils_submit'
get '/wizard/finish', controller: 'wizard', action: 'finish'
resources :wizard
第二页的表格(选择纸制品)
<%= form_for([@color], html: {multipart: true}, url: {action: "paper_submit"}, method: 'put') do |f| %>
<%= f.hidden_field(:color_scheme_id, value: @color.id) %>
<div class="row">
<% @product.where(enabled: true, color_scheme_id: @color).each do |product| %>
<% if product.tags.where(name: "Paper").count > 0 %>
<div class="col-lg-3 col-md-4 col-sm-6">
<%= check_box_tag 'ids[]', product.id -%>
<%= link_to image_tag(product.thumbnail.url(:thumbnail_small_opt), alt: product.name), product %>
<p><%= link_to product.name, product %>, $<%= product.price %></p>
</div>
<% end %>
<% end %>
</div>
<div class="row">
<div class="col-lg-12">
<p style="text-align:right;"><%= f.submit "Next: Select Pencils", class: "btn bg-primary" %></p>
</div>
</div>
<% end %>
到目前为止,一切正常。但是,问题是当用户在第三页上进行产品选择并到达第四页时。理想情况下,我希望我的应用程序将第二页中的产品ID参数与第三页中的不同产品ID组合在一起。
<%= form_for([@color], html: {multipart: true}, url: {action: "embellishments_submit"}, method: 'put') do |f| %>
<%= f.hidden_field(:color_scheme_id, value: @color.id) %>
<!-- A hidden field maybe?? This does not work though -->
<%= f.hidden_field(:paper_ids, multiple: true, value: @paper) %>
<div class="row">
<% @product.where(enabled: true, color_scheme_id: @color).each do |product| %>
<% if product.tags.where(name: "Pencil").count > 0 %>
<div class="col-lg-3 col-md-4 col-sm-6">
<%= check_box_tag 'ids[]', product.id -%>
<%= link_to image_tag(product.thumbnail.url(:thumbnail_small_opt), alt: product.name), product %>
<p><%= link_to product.name, product %>, $<%= product.price %></p>
</div>
<% end %>
<% end %>
</div>
<div class="row">
<div class="col-lg-12">
<p style="text-align:right;"><%= f.submit "Next: Purchase Your Selections", class: "btn bg-primary" %></p>
</div>
</div>
<% end %>
这似乎很简单,我只是想结合来自URL params的两个不同页面的数组,但我无法弄清楚如何做到这一点。并不是真的想要使用宝石,因为这是我需要弄清楚的。谢谢你的帮助。
答案 0 :(得分:3)
只需将变量放入session
:
def paper_submit
session[:paper_ids] = params[:ids]
respond_to do |format|
format.html { redirect_to wizard_pencils_path(
color_scheme_id: params[:color_scheme][:color_scheme_id]
) }
end
end
def pencils
@color = ColorScheme.find(params[:color_scheme_id])
@product = Product.all
@paper = Product.find(params[:id])
end
def pencils_submit
session[:pencil_ids] = params[:ids]
respond_to do |format|
format.html { redirect_to wizard_finish_path(
color_scheme_id: params[:color_scheme][:color_scheme_id]
) }
end
end
def finish
@all_products = Product.where(
enabled: true,
color_scheme_id: params[:color_scheme_id],
id: session[:paper_ids] + session[:pencil_ids]
)
end
finish.html.erb
:
<% @all_products.each do |product| %>
<%= product.name %>
<br />
<% end %>
答案 1 :(得分:1)
你应该添加这两个数组并制作如下内容:
def pencils_submit
respond_to do |format|
format.html { redirect_to wizard_finish_path(
color_scheme_id: params[:color_scheme][:color_scheme_id],
product_ids: params[:ids] + [:paper_ids]) }
end
end
然后以你的形式:
<% @product_ids.each do |id| %>
<%= f.hidden_field_tag 'product_ids[]', id %>
<% end %>
然后在你最后的完成动作做你想做的任何事情。