我的应用程序中有一个结帐序列,如下所示:
charges#address
- 有一个表格可以更新user
表中的送货地址charges#shipping
- 用户选择送货方式(更新数据库中的order.total
)charges#new
- 条纹实际上是收费我的问题是,由于某种原因,charges#shipping
(跳过第3步,从2到4)被完全跳过。每个步骤的信息如下。
第2步信息
表格:
<%= 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 %>
users_controller
中的方法应该将其发送到charges#shipping
页面,但最终会发送到charges#new
:
def update
@user = User.find(params[:id])
@user.update_attributes(account_update_params)
if @user.update(account_update_params)
redirect_to charges_shipping_path
else
render :back
flash[:notice] = "Something is amuck."
end
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :phone, :admin, :stripe_customer_id, :street_address_1, :street_address_2, :city, :state, :zip, :provence, :country, :has_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步中的表单(被跳过的表单,但这里真正重要的是它是form_for(@order)
:
<h3>Shipping Rates</h3>
<% shipping_choices = [] %>
<% @ups_rates.each do |rate| %>
<% choice = [] %>
<% choice << number_to_currency(rate[1]/100) %>
<% choice << number_to_currency(rate[1]/100).to_s + " - " + rate[0].to_s %>
<% shipping_choices << choice %>
<% end %>
<%= simple_form_for order_path(@order) do |f| %>
<div class="row">
<div class="form-inputs text-left">
<div class="form-group col-sm-6">
<%= f.collection_radio_buttons :shipping, shipping_choices, :first, :last, item_wrapper_class: :block_radio_button_collection %>
</div>
</div> <!-- form inputs -->
</div> <!-- choices row -->
<div class="row">
<%= f.button :submit, "Calculate Shipping" %>
</div>
<% end %>
这是order#update
方法:
def update
@order = current_order
if @order.update(order_params)
redirect_to charges_address_path
else
render :back
flash[:notice] = "Something is amuck."
end
end
对我来说这是一个非常奇怪的问题。谁能看到我出错的地方?
其他信息
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
答案 0 :(得分:0)
首先,您从UsersController#update
重定向到ChargesController#shipping
,它进入了运送操作,其中if @order.update(order_params)
的条件变为true,因为order_params在这里为零,因此它重定向到{{1 }}
当您在rails中执行redirect_to时,它会重定向为GET请求。当您提交表单时,默认情况下它将是POST / PUT(除非您更改它)。 您甚至可以在日志中看到所有重定向如何进入GET。
不建议对两者使用相同的操作。如果您确实想要使用相同的操作,请确保在其中添加ChargesController#new
或request.method=='GET'
的支票。
从我在您的运输操作代码中看到的内容,它被写入处理表单提交(POST)而不是通过重定向处理GET请求。 因此要么逻辑地处理它,要么将它分成单独的动作,一个用于呈现表单,另一个用于处理表单提交。
P.S。另外不要忘记添加&#39;并返回&#39;每次重定向后。
request.method=='POST'