我想知道如何从Rails控制器中的destroy动作中删除外部API中的某些资源。
删除此资源我必须这样做,实际上我在控制器文件夹中的关注中有这个代码:
def unsubscribe_user
@external_api = ExternalApi.new(@key1, @key2)
@subscription = @external_api.create(:subscription)
# subscription_id and client_id are stored in my DB as an "historical" record
@unsubscribed = @subscription.delete(subscription_id, client_id)
end
def destroy
unsubscribe_user
if @unsubscribed.nil?
flash[:notice] = "Successfully unsubscribed..."
redirect_to root_path
else
flash[:error] = "An error has ocurred..."
redirect_to root_path
end
end
sooo,在我看来,我有:
<p>Unsubscribe <%= button_to "Cancel my Subscription", suscription_path, {:action => :delete} %> </p>
Rails需要一个ID才能从DB中删除资源(例如:subscription_path(current_user.id)),因此,我没有在本地数据库中拥有此资源,也没有ID可以传递。我只需要在destroy动作中执行这个Concern代码。我怎样才能做到这一点?
我忘记了路线:
resources :subscribe, except: :edit
答案 0 :(得分:0)
只使用像1这样的假身份证,或者您可以使用其他路线配置来执行此操作
答案 1 :(得分:0)
在路线中使用单数资源:
resource :subscribe, except: :edit
然后在你的rake routes
中,你可以看到你不需要id参数