0

我有一个用户模型,除了标准帐户信息外,还包括运输信息。

我有三个不同的视图可以更新用户的信息:

  • 向导
  • 设置
  • 帐户

我知道我应该使用 REST 模型,因此我尝试通过 Update 操作运行所有这些更新。一个问题是,在向导视图中,我需要调用 Stripe API。所以我在 Update 控制器中有那个 Stripe 调用。但是现在当用户尝试在设置或帐户视图上更新他们的帐户时,更新控制器正在调用 Stripe API,这显然不是我想要的。

所以我试图做的是插入一个条件来检查是否正在传递向导参数之一(:total_value),如果是,则向Stripe收费。如果不是,那么只需更新用户数据。但我无法让它工作,它只是更新用户而不是向 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 %>
4

1 回答 1

0

我看到两个问题。首先是您的:

if !params[:total_value].nil?

应该

unless params[:user][:total_value].nil? (我个人更unless喜欢if !...

正如 Nishant 的评论中所述。其次,你有一个elsif条件的结束,没有它检查任何东西。一个else就够了。

于 2013-01-13T06:08:16.813 回答