我想添加取消Braintree订阅的链接。根据braintree参考,我必须使用const Private = (props) => {
return (
<main>
<h1>Private </h1>
<Link href="/">
<a>Home</a>
</Link>
</main>
);
}
Private.getInitialProps = async ({ res }) => {
// getInitialProps is ejecuted in server side when you refresh the page (first time)
// but if you navigate in client side it is ejecuted in client. Server side and client
// have a different session for API calls. Is posible to share the session by client
// and server side?
const response = await request.get('http://someDomain/private');
let body;
if (response.status !== 401) {
body = await response.json();
} else {
if (res) {
res.writeHead(302, { Location: '/login' })
res.end()
} else {
Router.push('/login')
}
}
return {}
}
export default Private;
为了达到这个目的,我使用以下代码:
视图/帐户/ show.html.erb
Braintree::Subscription.cancel(# here is placed the braintree_subscription_id)
accounts_controller.rb
<%= link_to 'Cancel Subscription', Braintree::Subscription.cancel(@braintree_subscription_id) %>
但是出现错误def show
@accounts = Account.find(params[:id])
@braintree_subscription_id = current_store.braintree_subscription_id
end
我做错了什么?有什么想法吗?
更新1
根据@Pavan代码更新代码后,单击取消订阅时记录。
undefined method to_model for #<Braintree::ErrorResult:0x007fee12083e98>
Did you mean? to_yaml
答案 0 :(得分:0)
您不能将 API方法作为路径提供给link_to
。您应为controller#action
定义自定义路径,并在方法中执行逻辑。
#routes.rb
get "/cancel_subscription/:subscription_id", to: "accounts#cancel_subscription", as: "cancel_subscription"
让link_to
像这样
<%= link_to 'Cancel Subscription', cancel_subscription_path(subscription_id: @braintree_subscription_id) %>
最后在控制器中
def cancel_subscription
result = Braintree::Subscription.cancel(params[:subscription_id])
redirect_to "your desired path"
end