将参数添加到由字符串定义的路径 - rails

adding params to a path defined by a string - rails

我有以下 link 将参数传递给 url,它工作正常。

<%= link_to "Buy", new_buyer_path(plan: 'item_D78387628dd', cost:'.00'), class: "btn btn-pink", role: "button" %>

但是,我有第二个 link,它的路径来自一个名为 raw_cml 的字符串 - raw_cml 在没有添加参数的情况下工作正常。

举个例子

raw_cml = dashboards/imp/budget_mgmt

<%= link_to "Move", raw_cml(score: '9', question:'8'), class: "btn btn-pink" %>

在此示例中,我收到以下错误:

undefined method `raw_cml'

有人可以帮助我朝着正确的方向前进吗?

我不确定,但您可以列出 Rails 应用的可用路径,例如:

bundle exec rails routes | grep link

检查 link_path 是否可以使用。

尝试用字符串 url:

连接参数
<% fixed_raw_cml = raw_cml + (raw_cml.include?('?') ? '&' : '?') %>
<%= link_to "Move", fixed_raw_cml + { score: '9', question:'8' }.to_query, class: "btn btn-pink" %>

我对此做了更多研究,发现 .to_params 给出了一种单行方法。

<%= link_to "Improve", raw_cml + '?' + { uqr: qid }.to_param, class: "btn btn-pink" %>

似乎给出了相同的结果。

我对这种方法的任何不可预见的后果感兴趣吗?

谢谢