user3646037
user3646037

Reputation: 293

Passing parameter from link_to to controller method

How can I pass subscription.title to my controller method watchsub? How do you then access the variable in watchsub?

In my view I have

<% @subscriptions.each do |subscription| %>
              <tr>
                <td> <%= link_to subscription.title, watchsub_stream_path(subscription.title), :method => :get %> </td>
              </tr>
            <% end %>

Upvotes: 4

Views: 879

Answers (1)

Mandeep
Mandeep

Reputation: 9173

First all you'll need a post request not a get as you want to send something. You have to make a route in your routes.rb file for you custom action like:

post "/sub/:id" => your_controller#watchsub , as: :watchsub    #Here id will be your subscription id

then you can make your link like this:

<%= link_to subscription.title, watchsub_path(subscription.id), :method => :post %>

and inside your watchsub method you can access that particular subscription by doing something like:

@subscription = Subscription.find(params[:id])

This will give you more flexibility as you'll have your subscription and you can access all its attributes for example if you want title then you can simply do

@subscription.title

Upvotes: 1

Related Questions