0

I am trying to create a link on the "index of tickets" to link to "edit" page. (Pls see picture below) Right now I can type "0.0.0.0:3000/tickets/1/edit" to go to edit page. However, i'm not sure how to link to this page. Would you pls give me some pointers?

Thank you for the guidance.

Note: I am learning RoR & creating this page based on raistutorial.org

I am stuck at the location of the following picture

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
Kevin H
  • 599
  • 1
  • 5
  • 10

4 Answers4

1

When you follow the convention of using resourceful routes in your config/routes.rb, according to http://guides.rubyonrails.org/routing.html, then you benefit for having nice path and url helpers available.

To see what paths are available, just run rake routes, and you'll see output like:

% rake routes

    tickets GET    /tickets(.:format)          tickets#index
            POST   /tickets(.:format)          tickets#create
 new_ticket GET    /tickets/new(.:format)      tickets#new
edit_ticket GET    /tickets/:id/edit(.:format) tickets#edit
     ticket GET    /tickets/:id(.:format)      tickets#show
            PUT    /tickets/:id(.:format)      tickets#update
            DELETE /tickets/:id(.:format)      tickets#destroy

From this, we can see that there's a named route edit_ticket, so we can use the edit_ticket_path or edit_ticket_url helpers (the latter will include the domain name, and is useful for things like emails).

It's useful to compare the output of rake routes to what you have in your config/routes.rb to make sure you have a good understanding of rails conventions and resourceful routes.

Ben Taitelbaum
  • 7,343
  • 3
  • 25
  • 45
0
<%= link_to 'edit', edit_ticket_path(ticket) %>
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
0
<%= link_to edit_ticket_path(ticket) %>

One tip how you could have found this out yourself: if you use scaffold, there's an edit link on the show.html.erb page - you could have just used this as an example exchanging the instance of ticket it is referring to.

Note: the :method option for link_to refers to the http method, which can be GET, PUT, POST, DELETE, but not the controller action!

bento
  • 2,079
  • 1
  • 16
  • 13
0

The :method attribute specifies the HTTP attribute, that's to say POST, GET, PUT, DELETE, UPDATE, HEAD and whatsoever.

Here you need to provide a path to the ticket edit link in the second parameter.

That can be done like this :

<%= link_to "edit", edit_ticket_path(ticket) %>

However when you do just <%= link_to "show", ticket %>, Rails automatically infers it's the show page you want.

Cydonia7
  • 3,744
  • 2
  • 23
  • 32
  • Thank you so much for the answer & explanation about :method attribute specifies HTTP attribute :) – Kevin H Jul 29 '12 at 03:07