以字符串的形式输出Rails 5.0键值路径
问题描述:
我对Rails很陌生,所以我会尽我所能解释这一点。我有一个简单的Products
模型,其中一个字段是条带计划名称的名称。这样可以添加新产品,并且选择将计划名称附加到路径末尾。它非常适合测试。以字符串的形式输出Rails 5.0键值路径
继承人我的products/show.html.erb
行问题。因为任何有经验的Rails开发人员都应该看到这个问题,所以我现在只包含这一点。
产品/ show.html.erb硬编码
...
<%= link_to 'Subscribe', new_subscription_path(plan: 'monthly'), class: 'btn btn-primary' %>
...
产品/ show.html.erb我需要什么
...
<%= link_to 'Subscribe', new_subscription_path(plan: '<%= @product.stripe_name %>'), class: 'btn btn-primary' %>
...
有<%= @ product.stripe_name% >作为一个字符串抛出一个错误。
期望得到的结果是这样的浏览器
http://127.0.0.1:3000/subscription/new?plan=monthly
答
此:
<%= link_to 'Subscribe', new_subscription_path(plan: '<%= @product.stripe_name %>'), class: 'btn btn-primary' %>
应该是这样的:
<%= link_to 'Subscribe', new_subscription_path(plan: @product.stripe_name), class: 'btn btn-primary' %>
你不把<%= ... %>
内另一个<%= ... %>
。
这不起作用,因为Stripe期望计划名称@product.stripe_name是一个字符串。 –
我尝试过,但它只是在浏览器'http://127.0.0.1:3000/subscription/new'中输出新路径 –
新手监督......我忘了将':stripe_plan'添加为允许的字段products_controller。 –