Route to default not found 'catch all' address - ruby-on-rails

I'm trying to route URLs like stats/non-existent-page to a default 'not found' page, where I'd like to display a 'Did you mean' suggestion along with a not found message, how can I route all non-existent URLs to a default controller/action?

Something like:
match 'stats/*path' => 'default#non_existent'
Add this after your other 'stats/..' urls , This will route your all stats/* paths to the controller action mentioned with path variable you can use to determine your 'did you mean' suggestion..

Related

How to make a URL displayed in Activeadmin clickable

In one of my ActiveAdmin pages I have fields containing a URL (either https: or file:). How can I make that when clicked on a new browser tab opens with the corresponding content ?
I tried
link_to('site web', :siteWeb)
but the result is an error message:
undefined method `siteWeb_path' for #<#<Class:0x00007f048201edb0>:0x00007f048202f700>
The problem here is your using a path helper that isn't defined in your routes.rb file. There's an easy way to see what the right path helper is though, just go to your terminal and run:
rails routes
You'll get a bunch of information about each route, along with the specific name of the route path helper function defined in Rails. Then you can just use it like so: https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Routing in Kohana without showing controller name

I want to create a routing in Kohana Framework Version 3.3.1.
I want URL like http://www.test.com/male/London and internally they will act like below URL-
http://www.test.com/list/search/London
I want to hide the controller and action name from the URL.
Any help is greatly appreciated.
This can be accomplished by using routes in bootstrap.php and/or a module's init.php file.
For example, you could set up a route for (male/<location>) and then your default controller would be list and the action search.
You could then access the location within the controller/action by using $this->request->param('location'); to be used in whatever DB query you need it.
Confused? Have a read through this section of Kohana Docs and it should all make sense.
You have to do two changes in your file:
Bootstarp file :
Route::set('list', 'male/<id>' )
->defaults(array(
'controller' => 'list',
'action' => 'search',
));`
And second is you can make your link like
href="<?php echo URL::site('male/'.id, TRUE) ?>">
And this route file should above at your default route file.

How to check for invalid route in layout view

I have this piece of code written in Application/view/layout/layout.phtml for adding a link which will be pointing to current page. This I added for debugging purpose for refreshing the current page.
<li class="active"><?php echo $this->translate('Refresh') ?></li>
This is working fine as long as I don't invalid route (404 error). I get below fatal error
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException'
with message 'No RouteMatch instance provided'
I wan't to handle this by adding a condition to check if its a 404 error or an invalid route before trying to render the url. I am not sure how to do it. I tried to look the source code of Zend\View\View class and ViewModel class to see if there is a way I can get error code or something which can tell its a 404 error.
Edit:
As a last resort I have adding the try catch block like below which is working fine but want to check if there is any elegant way
<?php
try{
$url = $this->url();
?>
<li class="active"><?php echo $this->translate('Refresh ') ?></li>
<?php
} catch (Exception $ex) {
}
?>
The exception message you are getting is not because the route is undefined but rather that the routeMatch parameter of the URL view helper, Zend\View\Helper\Url, has not been set.
Normally when using the URL helper you can provide the name of the route as the first argument. The helper will internally use the router and route match to generate the correct URL string based off the route configuration.
For example
echo $this->url('test'); // will create the route matching the test route name
The helper also allows you to provide no route name, $this->url(), in which case it will use the last matched route by default when creating theses routes. There is however one special case when you cannot rely on this; when the application cannot match a route, there will be no RouteMatch available.
So using this shorthand convenience method in the layout.phtml when the application tries to load the 404 error template (which is wrapped by the layout.phtml) the URL view helper will have no RouteMatch available and throw the error you received.
To fix this you should always provide a route name when calling the URL view helper in the layout or error scripts.

cakephp route setting

I want to set the cakephp route for URL like this: /param1/contorllername/actionname/ How can I do that, and if I do it, how do I use $this->redirect(array('controller'=>$controllername,'action'=>$actionname)) to redirect my page. Similarly how do I use $this->Html->link for this url.
you can define this in your routes.php
Router::connect(
'/:param1/:controller/:action',
array(),
array('pass' => array('param1'))
);
Then you can declare route like this
$this->redirect(array('controller'=>'posts','action'=>'view','param1'=>10))
This will redirect to /10/posts/view

Issue with Default and catchall routes

I define a lot of explicit routes. One of them is:
routes.MapRoute("default", "",
new { controller = "Home", action = "Index" });
At the end, I define a catchall route:
routes.MapRoute("PageNotFound", "{*url}",
new { controller = "Error", action = "Http404" });
If I go to the homepage http://localhost, then the http404 page is shown. And strangely, if I remove the catchall route, then the welcome page appears correctly.
Note also that I have a menu where I call Url.RouteUrl("default") and the link to the homepage is correctly generated.
So, why is my default route not activated when the catchall route exists?
Update: I'm using routes.RouteExistingFiles=true. If I remove it, then it works as expected. But I need it to be set to true. What's the problem here?
Thanks.
If you use "routes.RouteExistingFiles=true" it means it will route existing (physically exist) files as its own - so routing will be skipped for those. I think in your root website there is probably a "default.aspx" or "index.htm" or something like that.
Turning on RouteExistingFiles will then allow those files to be executed normally (instead of via routing).
Now I think what happen is that your catchall routing is overriding you RouteExistingFiles - so it automatically routes the default.aspx into your 404 catchall.
If you still have the default route (I.E. {controller}/{action}/{id}) in RegisterRoutes() it will trap all URLs that match the format of a normal MVC request.
In other words the catch-all route can only intercept a bad URL if it doesn't fit the normal format (blah/blah/blah/blah).
In the case of a non-existent controller the exception must be handled through conventional ASP.NET handling.
Theres a good description of handling this here
Did you try to put a constraint on the catch all route? Constraint should tell it that the catch-all segment should not have 0 characters.

Resources