Rails多个视图访问带有条件的控制器

时间:2013-01-13 04:29:07

标签: ruby-on-rails controller action

我的用户模型除标准帐户信息外还包含送货信息。

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

  • 向导
  • 设置
  • 帐户

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

所以我试图做的是插入一个条件来检查是否正在传入其中一个向导参数(:total_value),如果是,则向Stripe充电。如果不是,那么只需更新用户数据。但我无法让它工作,它只是更新用户而不是向Stripe收费。

这是分离这些更新的正确方向吗?或者我应该在Users控制器中创建自定义操作吗?这是如何工作的?

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 %>

1 个答案:

答案 0 :(得分:0)

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

if !params[:total_value].nil?

应该是

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

如Nishant的评论中所述。第二,你有一个elsif和那个条件的结束,没有检查任何东西。只需else即可。