0

I'm trying to destroy a link from a User Show view.

So from /users/1,

I want to access destroy action in teacher_student_links controller.

The first attempt:

<%= link_to 'destroy', :controller => "teacher_student_links", :action => "destroy", :id => @user.id %>

This fails because it routes to "show" action in the specified controller. So first question: why does it point to the "show" action instead of "destroy?" I tried to enclose the above with parenthesis, and got a meaningless error as well.

The second attempt:

in config/routes.rb

match '/bye_teacher' => 'teacher_student_links#destroy'

view

<%= link_to 'destroy', '/bye_teacher', :user_id => @user.id %>

also tried,

<%= link_to 'destroy', '/bye_teacher', :user_id => @user %>

Both lines correctly direct to the specified controller and action, but the parameter is not passed in. (can't find the user without an ID error)

second question: am I passing in the variable in a wrong way in this case?

It's little embarrassing to ask these two questions, but I wanted to know the reasons behind these problems.

UPDATE:

<%= link_to 'destroy', teacher_student_links_path(:user_id => @user), :method => :delete %>

gives

No route matches [DELETE] "/teacher_student_links"

<%= link_to 'destroy', teacher_student_links_path(@user), :method => :delete %>

gives

No route matches [DELETE] "/teacher_student_links.1"

... so I ran

rake routes and I got

DELETE /teacher_student_links/:id(.:format) teacher_student_links#destroy

Maximus S
  • 10,759
  • 19
  • 75
  • 154
  • Related: http://stackoverflow.com/questions/7465919/rails-link-to-method-geting-when-it-should-delete – ghoppe Jan 22 '13 at 05:36

2 Answers2

1
match "/bye_teacher/:id" => "teacher_student_links#destroy"


<%= link_to 'destroy', teacher_student_links_path(:id), 
:confirm => 'Are you sure you want to destroy this teacher?', :method => :delete %>

This should also work in the view:

<%= link_to 'Destroy', :action => 'destroy', :id => @user.id, :method => :delete %>

Rails Routing from the Outside In

ghoppe
  • 21,452
  • 3
  • 30
  • 21
  • In this case, you don't need to put the match line in the routes, right? – Maximus S Jan 22 '13 at 05:02
  • Probably. You can run `rake routes` from the command line to be sure the RESTful path exists. – ghoppe Jan 22 '13 at 05:05
  • your answer was actually what I tried the very first time. I ran into this error though: `No route matches [DELETE] "/teacher_student_links.1"` even when rake routes gives `DELETE /teacher_student_links/:id(.:format) teacher_student_links#destroy` I am confused why the path directs to `..links.1` instead of `..links/1` – Maximus S Jan 22 '13 at 05:10
  • @MaximusS yeah, your teacher_student_links_path isn't set up in routes. Does my second method work? – ghoppe Jan 22 '13 at 05:23
  • no. This is same as my first attempt, which is described in the question. :( – Maximus S Jan 22 '13 at 05:26
  • Odd. you're sure you added `:method => :delete`? – ghoppe Jan 22 '13 at 05:32
  • Yes.. I don't know why this is happening but that seems to direct me to show action: `The action 'show' could not be found for TeacherStudentLinksController` – Maximus S Jan 22 '13 at 05:38
0

You are giving wrong path

<%= link_to 'destroy', teacher_student_links_path(@user), :method => :delete %>

should be like this:

<%= link_to 'destroy', teacher_student_link_path(@user), :method => :delete %>

When you run 'rake routes' then 1st column will tell you the path

Shanky Munjal
  • 671
  • 5
  • 18