如何将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

09-25 16:24
查看更多