简体   繁体   English

Stripe Rails取消订阅缺少ID

[英]Stripe Rails Cancel Subscription missing ID

I think I have a problem in my routes or in my controller. 我认为自己的路线或控制器有问题。 I tried so much but I can't figure out how to fix this code. 我做了很多尝试,但我不知道如何解决此代码。
It says in ActionController: 它在ActionController中说:

No route matches {:action=>"destroy", :controller=>"payments", :order_id=>"sub_AzLXhqXdkvY0Z8"} missing required keys: [:id] 没有路线匹配{:action =>“ destroy”,:controller =>“ payments”,:order_id =>“ sub_AzLXhqXdkvY0Z8”}缺少必需的键:[:id]

Dashboard show.html.erb 仪表板show.html.erb

<% @services.each do |service| %>
  <%= link_to service_path(service), class: "panel-row" do %>
    <span class="panel-cell panel-highlight"><%= service.title %></span>
    <span class="panel-cell"><%= service.student.full_name %></span>

    #ActionController tells me here is my problem

    <span><%= button_to "Cancel Subscription",
    order_payment_path(service.orders.last.subscription), 
    :data => { :confirm => "Are you sure?" }, :method => :delete,
    class: "btn btn-danger" %></span>

    #

     <% end %>
  <% end %>
<% end %>

The order_payment_path goes to the payment destroy controller order_payment_path转到付款销毁控制器

dashboard_controller.rb dashboard_controller.rb

def show
  @user = current_user
  if @user.profile_picture.nil?
    @user.profile_picture = "default.jpg"
  end
  @students = Student.all
  @orders = Order.all
  @order = Order.where(state: 'active') # gives me all the active orders
    @hash = @order.where(customer: @user.customer_id).map do |hash|
    hash.service #creates an array of service objects
  end
  @surveys = Survey.all

  if @user.admin == true
    @services = Service.all
  else
    @services = @hash
  end
end

PaymentsController.rb PaymentsController.rb

before_action :set_active_order, only: [:destroy]
def destroy
  @user = current_user
  @subscription = Stripe::Subscription.retrieve(params[:subscription])
  @subscription.delete
  @order.destroy
  respond_to do |format|
    format.js do
      redirect_to dashboard_path, notice: "Successfully Deleted"
    end
    format.html do
      redirect_to dashboard_path
    end
  end
end

private

def set_active_order
  @order = Order.where(state: 'active').find(params[:order_id])
end

routes.rb routes.rb

devise_for :users
resources :students

resources :services do
  resources :reviews, only: [ :index, :new, :create ]
end
resources :reviews, only: [ :show, :edit, :update, :destroy ]

resources :surveys, only: [ :new, :create, :index, :show, :destroy ]

resources :users

resources :orders, only: [:show, :create] do
  resources :payments, only: [:new, :create, :destroy]
end

get 'dashboard' => 'dashboards#show'

root to: 'pages#home'

Sorry if my code is messy because of the naming. 抱歉,由于命名原因,我的代码很乱。 I appreciate your effort! 感谢您的努力!

Issue is in this url helper order_payment_path(service.orders.last.subscription) 此网址帮助order_payment_path(service.orders.last.subscription)问题是order_payment_path(service.orders.last.subscription)

Here you have two pass two arguments since it is nested path the first argument will correspond to your order object and second will correspond to your payment object something like this order_payment_path(service.orders.last, service.orders.last.subscription) 在这里,您有两个传递两个参数,因为它是嵌套路径,第一个参数将与您的order object相对应,第二个参数将与您的付款对象相对应,例如order_payment_path(service.orders.last, service.orders.last.subscription)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM