Ruby on Rails Path Helpers

Create route path helper

Resourceful routing does create create and destroy helpers, but they're implied by the type of HTTP request being made (POST and DELETE respectively) so the routing helper methods should work fine with the code you've provided.

Suppose you have the following route definition:

complicated_scope do
resources :my_resources
end
end

As a simple example, in the case of delete, you could use a named route like so:

link_to "Delete [resource]", complicated_scope_resource_path(id: @my_resource.id), method: :delete

Since the HTTP verb disambiguates the controller action this helper method routes to the destroy method of the controller.

Alternatively, you should be able to use the array syntax as well.

link_to "Delete [resource]",  [:complicated_scope, @my_resource], method: :delete

The same goes for forms:

<%= form_for [:complicated_scope, @my_resource] do |f| %>

If @my_resource is a new object (not persisted), as in the case of a new action this would be equivalent to sending a post request to /complicated_scope/my_resource with the form params going in the body of the request.

Alternatively if @my_resource exists, as in the case of an edit action, the above would be equivalent to sending a PUT/PATCH which will route to the update action of your controller with /complicated_scope/my_resource/:id/update.

Where is the documentation on url helpers in rails?

You can determine how many parameters a route helper requires by looking at the route definition.

For example, you might have this routes file:

resources :users

If you ran rake routes at the command line you would see something like this:

    users GET    /users(.:format)          users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy

The first column gives you the name of the route. You can append _path or _url to get the name of a route helper.

The third column shows the pattern. This is where you can figure out what the arguments are. Arguments are the parts prefixed with a colon, and optional arguments are shown in parentheses. For example the edit_user route has the pattern /users/:id/edit(.:format) which contains one required argument (id) and one optional argument (format), which tells me I need to pass at least one argument to the edit_user_path or edit_user_url helper:

edit_user_path(1) # => "/users/1/edit"
edit_user_path(2, :html) # => "/users/2/edit.html"

You can also use the argument names from the pattern as keys in a hash:

edit_user_path(id: 3, format: 'js') # => "/users/3/edit.js"

Finally, you can add extra arguments which will become part of the query string:

edit_user_path(id: 4, format: 'json', foo: 1) # => "/users/4/edit.json?foo=1"
edit_user_path(5, bar: 2) # => "/users/5/edit?bar=2"

See the Rails Routing Guide's section on Listing Existing Routes for more information about rake routes.

How to access *_path and *_url helpers in Rails 4

Running Rails 4, I get at them with:

app.root_path
=> "/"

app.users_url
=> "http://www.example.com/users"

rails path helper not recognized in model

You should be able to call the url_helpers this way:

Rails.application.routes.url_helpers.team_path(Team.first.id)

Rails: Check output of path helper from console

You can show them with rake routes directly.

In a Rails console, you can call app.post_path. This will work in Rails ~= 2.3 and >= 3.1.0.

Dynamic path helpers rails

This section might be helpful http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use

Verb    Path              Action     Helper

GET /photos index photos_path
GET /photos/new new new_photo_path
POST /photos create photos_path
GET /photos/:id show photo_path(:id)
GET /photos/:id/edit edit edit_photo_path(:id)
PUT /photos/:id update photo_path(:id)
DELETE /photos/:id destroy photo_path(:id)

If you want to create a helper for show action you can write

photo_path(@photo.id)

where @photo is your model object. Or you can pass @photo directly if it responds to id method.

photo_path(@photo)
edit_photo_path(@photo)

You can also load rails console (in terminal) and test routes using app like so app.photo_path(1) (it will show you the route for the photo with id equals 1)

Path helpers generate paths with dots instead of slashes

Yes, this is a pluralization error.

By passing the ID 1, I assume that you wish to display a single record.

So you need to use the singular 'message_thread':

message_thread_path(1)

Which will yield:

http://localhost:3000/message_threads/1


Related Topics



Leave a reply



Submit