Alter the browser URL in Rails - ruby-on-rails

Is there any way to change the browser URL of the request?
Lets say I've a '/articles/foo-bar' and I need to change this to '/articles/foo_bar'.
I've tried Routing Constraints and changed the request.path_info with no luck. I successfully changed the parameters but is not reflected in Browser URL.
PS: I want to replace '-' to '_' in browser URL in overall App.
EDITED:
Routes: get 'brands/:name-:id/articles', to: 'brands#articles'
Here name can be changed dynamically. And if a dash in name should be replaced by underscore.

You can use to_params method on your model
Please check this
http://blog.teamtreehouse.com/creating-vanity-urls-in-rails

Related

Rails wildcard route with full URL in param

I want to redirect to any given URL. I am trying this:
Route:
get 'track/*redirect_url', to: 'tracker#track'
Controller:
redirect_to params[:redirect_url]
However when I visit tracker URLs - I am getting strange redirects:
http://localhost:3000/http://google.com/search=xyz => http:/google.com/search=xyz (one slash is missing!)
http://localhost:3000/http://google.com => http:/google (slash + .com is missing"
Rails is apparently somehow transforming URL parts but since I do not have a control over it - I need a way to fix it.
Any ideas how?
It happens because rails tries to parse that as a single url and fails because of escaping (// is parsed as "escaped /").
Also what happens to .com - it's parsed by rails as format (like .html or .json).
I would suggest you doing it with URL encoding and query-string params, eg:
http://localhost:3000/track/?redirect_url=https%3A%2F%2Fgoogle.com

How to use external URL as path (Rails)

I am trying to create routes within an app that I am working on like the following example:
http://www.example.com/entrepreneur.com/article/251468
My hope is to basically load an external page into an iframe by adding our domain to the URL. It needs to be without storing the external url in a database because I need every website accessable in this way. How can I do this?
You need a route with a wildcard like this:
get 'url/*args', to: 'your_controller#your_action'
See http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments
I would suggest you namespace the route under some keyword to catch this wildcard route explicitly (hence url in the above).
You may need to tweak the route to allow periods to prevent them from becoming the format. I forget if that's true for these or not.

FormIt - redirect hook issue

ISSUE: I'm using FormIt and the redirect hook is not working for me.
It is redirecting to http://www.example.comPageName.html instead of http://www.example.com/PageName.html. It is not placing the forward slash after the page name.
Has anyone seen this issue before?
Here is my FormIt snippet.
[[!FormIt? &hooks=`email,redirect` &emailTpl=`MyEmailChunk` &emailTo=`mypersonalemail#email.com` &emailSubject=`Contact Us form submission` &redirectTo=`35` &emailFrom=`info#domainnamme.com`]]
Yes, the MyEmailChunk exists.
The email addresses are dummy values
The resource with id 35 is in the same context and is published and is hidden from the menus.
I'm using friendly urls.
If I try another resource it works.
URL generated in the following code
$url = $this->modx->makeUrl($this->formit->config['redirectTo'],$contextKey,$redirectParams,'full');
'full' means URL is absolute, prepended with site_url from config ( http://rtfm.modx.com/display/revolution20/modX.makeUrl ). Please check your site_url variable in /core/config/config.inc.php
Actually the site_url variable needs to be checked in the System Settings.
Make sure you have a trailing slash.

Symfony use_javascript() routing issue

i am using symfony 1.4.11; use_helper('Url').
On using link_to('new',course/course/type/new),
the url it show is ../backend_dev/backend_dev/Course/course/type/new
instead of
../backend_dev/Course/course/type/new.
Same issue exist for form_tag also.
Edit
Above issue was solved.By setting no_script_name: true at config and clearing cache.
But image_tag(),use_stylesheet() and use_javascript() gives path as for example
use_javascript('jquery-1.6.1.min.js')
==>../web/backend_dev/js/jquery-1.6.1.min.js
instead of
use_javascript('jquery-1.6.1.min.js') ==>../web/js/jquery-1.6.1.min.js
Any help appreciated.
Hard to say without your full routing.yml but the one thing i see is that your internal_uri should be expressed as an abs url with a query string like:
link_to('new','/Course/course?type=new');
Note the forward slash at the beginning. Also the module name should be the real module name, not the routed one so if the maodule is /apps/backend/modules/Course then the module in the internal URI should be Course not course same with the action name.
If the route is named then you should use one of the following:
link_to('new','#routename?type=new');
OR
link_to('new','routename', array('type'=>'new'));

How does one escape the # sign in a Url pattern in UrlMappings.groovy?

In order to maintain the current set of Urls in a project, I have to be able to use the # (pound sign) in the Url. For some reason the pound sign does not appear to work normally in this project for UrlMappings.groovy.
Is there a special escape-sequence that must be used when placing # signs in UrlMappings.groovy?
Am I missing some reason why one cannot use pound signs at all?
In the following URL Mapping example, the browser goes to the correct page, but the pageName variable is null:
"/test/${urlName}#/overview"(controller:'test', action:'overview') {
pageName = "overview"
}
I thought everything after # in the url would be treated on the client side of the browsers where it tries to find a and scroll to that location.
If you dump the request containing the pound char, do you even see the data behind #?
I used a Named URL mapping and it works fine, no need to escape the "#" sign:
name test: "/#abc" (controller: 'test', action:'homepage')
EDIT: My above answer is wrong. In fact, it falls to a special case when homepage is the default action of the view.
Netbrain is right, the path after "#" will never be sent to server. In stead, I found that it's possible using "%23" instead of "#". Please take a look at here.
For example, instead of /test#/abc we should use /test%23/abc as URL mapping (both at client side & server side).

Resources