我想我的路线或控制器都有问题。我尝试了很多,但我无法弄清楚如何修复此代码 它在ActionController中说:
没有路线匹配{:action =>“destroy”,:controller =>“payment”, :order_id =>“sub_AzLXhqXdkvY0Z8”}缺少必需的键:[:id]
仪表板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 %>
order_payment_path转到付款销毁控制器
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
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
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'
很抱歉,由于命名,我的代码很乱。我感谢您的努力!
答案 0 :(得分:0)
问题出在这个url帮助器中
order_payment_path(service.orders.last.subscription)
这里有两个传递两个参数,因为它是嵌套路径
第一个参数对应于您的order object
,第二个参数对应于您的付款对象
像这样order_payment_path(service.orders.last, service.orders.last.subscription)