Ruby on rails Rails将_重定向到页面显示之前发生的事件
我的应用程序中有一个结账顺序,如下所示:Ruby on rails Rails将_重定向到页面显示之前发生的事件,ruby-on-rails,Ruby On Rails,我的应用程序中有一个结账顺序,如下所示: 购物车-计算用户的税款,然后点击结账按钮 费用#地址-在用户表中有一个表单可以更新发货地址 费用#配送-用户选择配送方式(更新数据库中的订单.总计) 收费#新增-Stripe实际收费 我的问题是,出于某种原因,费用35;运输(步骤3被跳过,从2到4)被完全跳过。每个步骤的信息如下所示 步骤2信息 As requested, here is my (unfortunately long) `charges#shipping` method: def sh
费用#地址
-在用户
表中有一个表单可以更新发货地址费用#配送
-用户选择配送方式(更新数据库中的订单.总计
)收费#新增
-Stripe实际收费费用35;运输
(步骤3被跳过,从2到4)被完全跳过。每个步骤的信息如下所示
步骤2信息
As requested, here is my (unfortunately long) `charges#shipping` method:
def shipping
@user = current_user
@products = current_order.order_items.all
@order = current_order
if @order.update(order_params)
new_total = @order.total + @order.shipping
@order.update_attributes(total: new_total)
redirect_to new_charge_path
else
render :back
flash[:notice] = "Something is amuck."
end
@envelope_weight = 0
@not_envelope = 0
@products.each do |thing|
if thing.product.envelope
@envelope_weight += thing.product.weight
else
@not_envelope += 1
end
end
if @not_envelope == 0
# shipping for envelope
else
packages = []
@products.each do |thing|
unless thing.product.envelope
if thing.id == 1
packages << ActiveShipping::Package.new( (thing.product.weight + @envelope_weight ) * 16,
[thing.product.box_length, thing.product.box_width, thing.product.box_depth],
units: :imperial)
else
packages << ActiveShipping::Package.new( thing.product.weight * 16,
[thing.product.box_length, thing.product.box_width, thing.product.box_depth],
units: :imperial)
end # envelope weight if else
end #unless
end ## each do
end # not envelope if/else
origin = ActiveShipping::Location.new( country: 'US', state: 'CO', city: 'Sedalia', zip: '80135')
if @user.country == 'US'
destination = ActiveShipping::Location.new( country: @user.country, state: @user.state, city: @user.city, zip: @user.zip)
else
destination = ActiveShipping::Location.new( country: @user.country, province: @user.state, city: @user.city, postal_code: @user.zip)
end # if/else for country
ups = ActiveShipping::UPS.new(login: 'lizbayardelle', password: 'UPSpassw0rd', key: '3D287D7B39D0D398')
ups_response = ups.find_rates(origin, destination, packages)
@ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
usps = ActiveShipping::USPS.new(login: '380LINCH6422')
usps_response = usps.find_rates(origin, destination, packages)
@usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
end
表格:
<%= simple_form_for(@user, url: user_path(@user), html: { method: :put }) do |f| %>
<div class="form-inputs text-left">
<div class="form-group col-sm-6">
<%= f.label :street_address_1 %>
<%= f.text_field :street_address_1, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= f.label :street_address_2 %>
<%= f.text_field :street_address_2, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= f.label :city %>
<%= f.text_field :city, class: "form-control" %>
</div><div class="form-group col-sm-3 col-xs-6">
<%= f.label :state %>
<%= f.text_field :state, class: "form-control" %>
</div><div class="form-group col-sm-3 col-xs-6">
<%= f.label :zip %>
<%= f.text_field :zip, class: "form-control" %>
</div><div class="form-group col-sm-6">
<%= f.label :provence %>
<%= f.text_field :provence, class: "form-control" %>
</div><div class="form-group col-sm-6">
<%= f.label :country %>
<%= f.text_field :country, class: "form-control" %>
</div><div class="form-group">
<%= f.hidden_field :has_shipping, value: true %>
</div>
</div> <!-- form inputs -->
<%= f.button :submit, "Calculate Shipping" %>
<% end %>
以下是服务器中发生的情况:
Started PUT "/users/1" for ::1 at 2017-05-16 10:04:10 -0700
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R2ZDErL9UQ6nPNyYDI9FDeOp0x29t+/STWIkTKeShz2WWdO8kWu+z6V5NS/EKI6uiLcCAQBbwbsRMlhGs8v2VA==", "user"=>{"street_address_1"=>"10 Oak View Drive", "street_address_2"=>"Test", "city"=>"Aliso Viejo", "state"=>"CA", "zip"=>"92656", "provence"=>"", "country"=>"US", "has_shipping"=>"true"}, "commit"=>"Calculate Shipping", "id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
(0.1ms) commit transaction
(0.0ms) begin transaction
(0.0ms) commit transaction
Redirected to http://localhost:3000/charges/shipping
Completed 302 Found in 5ms (ActiveRecord: 0.3ms)
Started GET "/charges/shipping" for ::1 at 2017-05-16 10:04:10 -0700
Processing by ChargesController#shipping as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Order Load (0.1ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 5]]
CACHE (0.0ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 5]]
(0.1ms) begin transaction
OrderItem Load (0.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."order_id" = ? [["order_id", 5]]
Product Load (0.0ms) SELECT "products".* FROM "products" WHERE "products"."active" = ? AND "products"."id" = ? LIMIT 1 [["active", "t"], ["id", 2]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."active" = ? AND "products"."id" = ? LIMIT 1 [["active", "t"], ["id", 1]]
(0.0ms) commit transaction
156.95
(0.0ms) begin transaction
(0.0ms) commit transaction
Redirected to http://localhost:3000/charges/new
CACHE (0.0ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."order_id" = ? [["order_id", 5]]
CACHE (0.0ms) SELECT "products".* FROM "products" WHERE "products"."active" = ? AND "products"."id" = ? LIMIT 1 [["active", true], ["id", 2]]
CACHE (0.0ms) SELECT "products".* FROM "products" WHERE "products"."active" = ? AND "products"."id" = ? LIMIT 1 [["active", true], ["id", 1]]
Completed 302 Found in 1965ms (ActiveRecord: 0.6ms)
Started GET "/charges/new" for ::1 at 2017-05-16 10:04:12 -0700
Processing by ChargesController#new as HTML
Order Load (0.1ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 5]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 5]]
Rendered charges/_shipping.html.erb (2.1ms)
Rendered charges/new.html.erb within layouts/application (3.1ms)
CACHE (0.0ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 5]]
(0.1ms) SELECT COUNT(*) FROM "order_items" WHERE "order_items"."order_id" = ? [["order_id", 5]]
CACHE (0.0ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 5]]
OrderItem Load (0.1ms) SELECT "order_items".* FROM "order_items" WHERE "order_items"."order_id" = ? [["order_id", 5]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."active" = ? AND "products"."id" = ? LIMIT 1 [["active", "t"], ["id", 2]]
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."active" = ? AND "products"."id" = ? LIMIT 1 [["active", "t"], ["id", 1]]
Rendered layouts/_cart_text.html.erb (6.1ms)
Completed 200 OK in 82ms (Views: 80.6ms | ActiveRecord: 0.6ms)
这是步骤3中的表单(被跳过的表单,尽管这里真正重要的是它是(@order)的表单):
这对我来说是一个非常奇怪的问题。有人能看出我错在哪里吗
其他信息
As requested, here is my (unfortunately long) `charges#shipping` method:
def shipping
@user = current_user
@products = current_order.order_items.all
@order = current_order
if @order.update(order_params)
new_total = @order.total + @order.shipping
@order.update_attributes(total: new_total)
redirect_to new_charge_path
else
render :back
flash[:notice] = "Something is amuck."
end
@envelope_weight = 0
@not_envelope = 0
@products.each do |thing|
if thing.product.envelope
@envelope_weight += thing.product.weight
else
@not_envelope += 1
end
end
if @not_envelope == 0
# shipping for envelope
else
packages = []
@products.each do |thing|
unless thing.product.envelope
if thing.id == 1
packages << ActiveShipping::Package.new( (thing.product.weight + @envelope_weight ) * 16,
[thing.product.box_length, thing.product.box_width, thing.product.box_depth],
units: :imperial)
else
packages << ActiveShipping::Package.new( thing.product.weight * 16,
[thing.product.box_length, thing.product.box_width, thing.product.box_depth],
units: :imperial)
end # envelope weight if else
end #unless
end ## each do
end # not envelope if/else
origin = ActiveShipping::Location.new( country: 'US', state: 'CO', city: 'Sedalia', zip: '80135')
if @user.country == 'US'
destination = ActiveShipping::Location.new( country: @user.country, state: @user.state, city: @user.city, zip: @user.zip)
else
destination = ActiveShipping::Location.new( country: @user.country, province: @user.state, city: @user.city, postal_code: @user.zip)
end # if/else for country
ups = ActiveShipping::UPS.new(login: 'lizbayardelle', password: 'UPSpassw0rd', key: '3D287D7B39D0D398')
ups_response = ups.find_rates(origin, destination, packages)
@ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
usps = ActiveShipping::USPS.new(login: '380LINCH6422')
usps_response = usps.find_rates(origin, destination, packages)
@usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
end
根据要求,以下是我的(很遗憾很长)`费用#运输'方法:
def运输
@用户=当前用户
@产品=当前订单。订单项目。全部
@订单=当前订单
如果@order.update(订单参数)
新订单总数=@order.total+@order.shipping
@订单。更新_属性(总计:新_总计)
重定向到新的充电路径
其他的
渲染:返回
flash[:notice]=“有些东西乱七八糟。”
结束
@信封重量=0
@非(u)信封=0
@产品。每件事|
if thing.product.信封
@信封重量+=物品.产品.重量
其他的
@非U信封+=1
结束
结束
如果@not_信封==0
#邮寄信封
其他的
软件包=[]
@产品。每件事|
除非是东西、产品、信封
如果thing.id==1
包裹首先,您从UsersController#update
重定向到ChargesController#shipping
,它进入了shipping操作,其中if@order.update(order#params)
的条件变为true,因为这里的order#params为零,因此它重定向到charges controller#new
当您在rails中重定向到时,它将作为GET请求重定向。当您提交表单时,默认情况下它将是POST/PUT(除非您更改它)。
您甚至可以在日志中看到,所有重定向都是如何以GET的形式进行的
不建议对两者使用相同的操作。如果确实要使用相同的操作,请确保在它们中添加对request.method=='GET'
或request.method=='POST'
的检查
从我在您的发货操作代码中看到的情况来看,它是用来处理表单提交(POST)的,而不是用来通过重定向处理GET请求的。
因此,要么以逻辑方式处理它,要么将其分成不同的操作,一个用于呈现表单,另一个用于处理表单提交
另外,不要忘记在每次重定向后添加“并返回”
redirect_to charges_shipping_path and return
你能分享ChargesController的代码吗?#实际发生不必要重定向的地方。@rohan,是的,这很重要…添加了!这是很多好信息。我将@order=current\u order
复制到charges controller中的一个新的update\u order
方法中,并将if/else语句从shipping method和它正式允许我查看页面!我将选择此答案,因为您肯定解决了手头的问题,但如果您能提供有关如何构建新的update\u order
方法的帮助,我将不胜感激!
As requested, here is my (unfortunately long) `charges#shipping` method:
def shipping
@user = current_user
@products = current_order.order_items.all
@order = current_order
if @order.update(order_params)
new_total = @order.total + @order.shipping
@order.update_attributes(total: new_total)
redirect_to new_charge_path
else
render :back
flash[:notice] = "Something is amuck."
end
@envelope_weight = 0
@not_envelope = 0
@products.each do |thing|
if thing.product.envelope
@envelope_weight += thing.product.weight
else
@not_envelope += 1
end
end
if @not_envelope == 0
# shipping for envelope
else
packages = []
@products.each do |thing|
unless thing.product.envelope
if thing.id == 1
packages << ActiveShipping::Package.new( (thing.product.weight + @envelope_weight ) * 16,
[thing.product.box_length, thing.product.box_width, thing.product.box_depth],
units: :imperial)
else
packages << ActiveShipping::Package.new( thing.product.weight * 16,
[thing.product.box_length, thing.product.box_width, thing.product.box_depth],
units: :imperial)
end # envelope weight if else
end #unless
end ## each do
end # not envelope if/else
origin = ActiveShipping::Location.new( country: 'US', state: 'CO', city: 'Sedalia', zip: '80135')
if @user.country == 'US'
destination = ActiveShipping::Location.new( country: @user.country, state: @user.state, city: @user.city, zip: @user.zip)
else
destination = ActiveShipping::Location.new( country: @user.country, province: @user.state, city: @user.city, postal_code: @user.zip)
end # if/else for country
ups = ActiveShipping::UPS.new(login: 'lizbayardelle', password: 'UPSpassw0rd', key: '3D287D7B39D0D398')
ups_response = ups.find_rates(origin, destination, packages)
@ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
usps = ActiveShipping::USPS.new(login: '380LINCH6422')
usps_response = usps.find_rates(origin, destination, packages)
@usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
end
redirect_to charges_shipping_path and return