The problem I currently have is, that I got some routes for my API that I want to group such for each route of that group, some authentication middleware can execute. Let's say I have a list of nodes, then my current implementation looks like this:
router
.get('/nodes', authMiddleware, getNodes)
.post('/node', authMiddleware, getNode)
.get('/node/:id', authMiddleware, getSingleNode)
.put('/node/:id', authMiddleware, updateNode)
Is there an shorter way for using the authMiddleware for each of this route?
Use .use method
router.use(authMiddleware)
router
.get('/nodes', getNodes)
.post('/node', getNode)
.get('/node/:id', getSingleNode)
.put('/node/:id', updateNode)
Related
I'm new to rail. I have few clarifications, which I have listed below.
I have referred many links and stack overflow questions, everywhere it's mentioned to use request and fetch the details regarding the path, controller, action etc. but if I use request in my routes.rb it throws undefined local variable or method error.
I used constraints in my routes.rb and from there it calls a method matches? from a class where dynamic constraints are defined inside lib/constraints directory. In here the matches?(request) receives a parameter named request, which has details about current route, from where the parameter value is sent?, I have this doubt because while using this method inside routes.rn in constraint(ClassName) I'm not sepecifying the method name (matches?) or the parameter request
I would like to know the working of things behind the scene.
Thank You
Are you looking for it
request.url
# => "http://localhost:3000/lists/7/items"
request.path
# => "/lists/7/items"
If you have URIs:
localhost:3000/users/:id
localhost:3000/users
localhost:3000/users/new
....
You can do something like
rake routes | grep user
in order to get only those that contain user in its path name.
Hope this helps..
conventionally you don't access the current path in the routes.rb, at least as how it seems you're asking to access it. There are a number of different matching methods and techniques used in the rails router that serve the utility I imagine your looking for.
Here's a link to the docs: https://guides.rubyonrails.org/routing.html
Can you share exactly why you want to access the curret route in the routes.rb file?
I have a rails application which, among other things, provides a simple wrapper around API calls to a third-party service. I want to set up a route which starts with /api, but anything added on to the end of it is taken as a string variable. For example, if a client requests:
/api/apps/guid/details
...then I want to invoke the index action of the controller ApiController and make the string /apps/guid/details available to it.
I have read through the documentation on controllers and routes, but everything seems to assume that /apps/guid/details will be resources within my app, when actually I don't care about the structure of anything after /api.
How can I set up a route which allows me to do this?
You can use globbing in your config/routes.rb:
get "/api/*path", to: "api#index"
Which would be accessible in the controller via params[:path]
Details can be found in the rails guide.
I use require_admin! frequently in my controllers. It works great.
Now, I want to add named route like so:
# config/routes.rb
match "poniesandrainbows" => redirect("https://poniesandrainbows.com")
# ^sadly, not really a website, btw.^
How do I restrict access to that route? Is this possible? Obviously it redirects to a public URL, but I still want to keep the route private.
You cannot restrict access from routes.
The safest way to match "poniesandrainbows" with a controller where you can use require_admin! and then redirect them to the public url.
You can try to solve the problem on the front end. Maybe only show the link to admin users.
It won't stop other users to paste the link directly to their browser url though
That kind of functionality should be encapsulated in the controller. The router handles the plumbing of passing a request to the correct controller. It is the controller's job to correctly figure out how to process the request. In this cause, the controller would use the auth service (such as require_admin!) to determine if the user is allowed to be redirected or if they are doom to another fate.
It is actually possible although as the other posters mentioned very rarely a good idea. You can read about how in this blog post: (scroll down to the routes section)
http://collectiveidea.com/blog/archives/2011/05/31/user-centric-routing-in-rails-3/
Just as you figure out the route when the browser hits the webpage in Rails, how would you resolve it on the server side?
For example I want to return a URL to a RESTful resource called Bookmark in an API call and want to return the 'show' action of it, and I know that:
Bookmark id: 12
Then I want to resolve it to a string:
'/bookmarks/edit/12'
so that I can get this from my Model for example.
How would I go about doing this?
Thanks!
Pretty much everywhere in the views/controllers you can use route helpers to DRY up route references.
In models, you'll need to explicitly call the route helper like so.
Rails.application.routes.url_helpers.edit_bookmark_path(id) # => '/bookmarks/12/edit'
When using the default resourceful route generator method in routes.rb like
resource :bookmarks
I'm not sure I understand - your server is the thing that's making all of those routes work - the client's browser isn't figuring out what the route is - you application is doing it.
The paths to your resources are available as helper methods at all times (at least within the controllers, and views). As such, you should return the string as the body of a response in one of your actions, in the controller that's handling your API calls.
Check your rake routes on the command line, and you'll see a list of them. In the case of your example above, it would likely be called edit_bookmark_path(12)
How do I efficiently enable root-level routing with Ruby on Rails?
For example, instead of having:
/questions/a-question-here
I want:
/a-question-here
What technology would I use, and how would I configure the routing to enable this?
EDIT: I also have other models such as videos and users, so I'd like the routing to match other controllers as well. For example, how do I get /username to go to some action in the Users controller and /some-video-title to go to some action in the Videos controller?
Thanks for all your help, people.
Rails has a very configurable routing layer built in. Good documentation here:
http://guides.rubyonrails.org/routing.html
Your route could end up looking something like this:
match ":id" => "questions#show"
Depending on where you put that, it could override a lot of other routes, so be aware of that.