我有一个用户模型,除了标准的帐户信息之外,还包括运送信息。导轨访问带条件的控制器的多个视图
我有一个可以更新用户的信息三种不同的观点:
- 向导
- 设置
- 帐户
我知道,我应该使用REST模型,所以我尝试通过更新操作来运行所有这些更新。唯一的问题是在向导视图中,我需要调用Stripe API。所以我在Update控制器中有条纹调用。但是现在,当用户尝试在设置或帐户视图上更新其帐户时,更新控制器正在调用Stripe API,这显然不是我要做的。
因此,我试图做的是插入一个条件来检查是否向其中一个向导参数(:total_value)传入,如果是,则向带区充电。如果不是,那就更新用户数据。但是我一直无法让它工作,只是更新用户而不向Stripe收费。
这是分离这些更新的正确方向吗?或者我应该在用户控制器中创建自定义操作?这个怎么用?
users_controller
def update
if !params[:total_value].nil?
if @user.update_attributes(params[:user])
# flash[:success] = "Profile updated"
sign_in @user
# Value input by the user (in dollars)
@userPrice = params[:user][:total_value]
# Send full price in cents to Stripe
@stripePrice = @userPrice.to_i * 100
Stripe::Charge.create(
:amount => @stripePrice,
:currency => "usd",
:card => params[:user][:stripe_card_token],
:description => "Charge for service"
)
redirect_to success_path
flash[:success] = "Hurray!"
else
flash.now[:notice] = "Can not be saved, please enter information."
render :new
end
elsif
if @user.update_attributes(params[:user])
sign_in @user
flash[:success] = "Profile updated"
redirect_to account_path
else
flash[:error] = "Error"
end
end
end
向导/ show.html.erb
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :total_value, placeholder: "0", :class => 'amount' %>
<%= f.label :name %>
<%= f.text_field :name, required: "required" %>
<%= f.label :address_1, "Address" %>
<%= f.text_field :address_1, required: "required" %>
<%= f.label :address_2, "Address line 2" %>
<%= f.text_field :address_2 %>
<%= f.label :city, "City" %>
<%= f.text_field :city, required: "required" %>
<%= f.label :state, "State" %>
<%= f.text_field :state, required: "required" %>
<%= f.label :zip, "Zip code" %>
<%= f.text_field :zip, required: "required" %>
<%= f.hidden_field :stripe_card_token %>
<%= label_tag :card_number, "Credit Card Number " %>
<%= text_field_tag :card_number, nil, name: nil %>
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers_true: true}, {name: nil, id: "card_month"}%>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}%>
<%= f.submit "Update your shipping information", class: "btn btn-large btn-primary" %>
<% end %>
用户/ shipping.html.erb
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :address_1, "Address" %>
<%= f.text_field :address_1 %>
<%= f.label :address_2, "Address line 2" %>
<%= f.text_field :address_2 %>
<%= f.label :city, "City" %>
<%= f.text_field :city %>
<%= f.label :state, "State" %>
<%= f.text_field :state %>
<%= f.label :zip, "Zip code" %>
<%= f.text_field :zip %>
<%= f.submit "Update your shipping information", class: "btn btn-large btn-primary" %>
<% end %>
个用户/ account.html.erb
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
在控制器中,您正在检查'!params [:total_value] .nil?'。但它不应该是'!params [:user] [:total_value] .nil?' – Nishant
是的。那样做了!非常感谢!我想我误解了参数是如何工作的。 (我是新来的)你能指点我到哪里去了解更多? –