如何将subscription.title
传递给控制器方法watchsub
?然后如何访问watchsub
中的变量?
我认为我有
<% @subscriptions.each do |subscription| %>
<tr>
<td> <%= link_to subscription.title, watchsub_stream_path(subscription.title), :method => :get %> </td>
</tr>
<% end %>
最佳答案
首先,您需要发帖请求,而不是要发送的东西。您必须在routes.rb文件中为您的自定义操作创建一条路由,例如:
post "/sub/:id" => your_controller#watchsub , as: :watchsub #Here id will be your subscription id
那么您可以像这样创建链接:
<%= link_to subscription.title, watchsub_path(subscription.id), :method => :post %>
在watchsub方法中,您可以通过执行以下操作来访问特定的订阅:
@subscription = Subscription.find(params[:id])
这将为您提供更大的灵活性,因为您将获得订阅,并且可以访问其所有属性,例如,如果您想要标题,则只需
@subscription.title