Colton Seal
Colton Seal

Reputation: 379

How to call a certain path in delete method Rails

Typically when I am using a delete link it will look something like this

<% @variable_name.each do |block| %>
  <%= link_to 'Destroy', block, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>

For the current project, I am unable to use the "block" path for delete. I now need to call a path that is related to block, but not defined in it. I was thinking something like this

<% @variable_name.each do |block| %>
  <%= link_to 'Destroy', someother_path, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>

Is it possible to link to a url instead of using a helper method? If so how would I find that url.

Upvotes: 3

Views: 1077

Answers (2)

East2d
East2d

Reputation: 2096

Yes, you can use any other urls as long as you defined the routes in routes.rb

in your routes.rb file

resources :variables do
  delete :someother, on: :member
end

then you can check the routes. To check the urls, please run following command line in terminal.

rake routes

or

rake routes | grep variable

and you can do

<% @variable_name.each do |block| %>
  <%= link_to 'Destroy', someother_variable_path(Variable.find_by_name(block)), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>

Upvotes: 1

MrYoshiji
MrYoshiji

Reputation: 54882

You can pass arguments to the path helper:

someother_path(first_argument: 'whatever')

Also, if you want the full path of the link (including domain host + port), use this:

someother_url(first_argument: 'whatever')

The last option is to write the path as a string, for example:

link_to 'Destroy', "/posts/#{post.id}/destroy" # etc.

But this last option should not be used as it is complicated to maintain.

Upvotes: 0

Related Questions