frames routing in erb rails - ruby-on-rails

Two questions.
Number 1:
This is how I am creating the frames in .erb files.
<frameset rows="170,*" frameborder="0" border="0" framespacing="0">
<frame name="topNav" src="top_nav.html">
</frame>
</frameset>
I have couple more frames inside above frameset. If I run this code, I get "No route matches [GET] "/top_nav.html". Question is: Is it really necessary to create/config routes of all the src locations specified in the frame tag?
Number 2:
In my application, I would need to change links in my app very frequently. If I need to consider the routing, I would need to create routes more frequently. Right? How can I avoid consider the routes? Basically, If I add any link in my app, it should work irrespective of fact that whether I have added the routes or not.

You can put a static html page in your /publics folder, which will bypass rails routing and be served directly if e.g. someone visits website.com/top_nav.html, it'll serve up public/top_nav.html. The reason you're getting this error without visiting it is because the frame tries to load the src when you load the frame.
If you just want to serve a blank page as a catch-all for missing pages, you could create a controller that catches anything without another matching route, like so:
# config/rotues.rb
# make sure this is at the BOTTOM of your routes.rb
match "*path", to: "application#default", via: :all
And then in your application controller:
def default
render nothing: true
end
This probably isn't the best way to handle it, though. You'd be best having the links you want created and served dynamically, but that'd depend on what your application is doing.

Related

CakePHP 2.x: How can I overwrite an URL pointing to the index page?

I'm getting this issue:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
This will show www.mysite/controller/login as the site URL
I would like to overwrite www.mysite.com/controller/login with just www.mysite.com, but still go to the login page. Does anyone know how to do it with Cake 2.x?
The behavior It's not exactly as you describe.
What the following does:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
is allow you to type www.mysite.com in your browser, and get the view that www.mysite.com/controller/login renders.
It works like an url rewrite instead of a redirect. Therefore, the above should work as expected. However, if it's not an example, try to name your controller differently, as it may cause trouble with CakePHP.
As stated by Inigo Router::connect() just connects a route/URL to a controller action. So with your defined route you should be able to goto www.mysite.com and your login action will be served (although I'm not sure that it is a good idea to have the base URL act as the login page).
It does not prevent www.mysite.com/controller/login from working as this is one of CakePHP's default routes.
To disable the default routes you need to remove this line from routes.php:-
require CAKE . 'Config' . DS . 'routes.php';
Be warned, if you remove this line you must have defined routes for all your pages in your app's routes file. This is not necessarily a bad thing, Beware the Route to Evil is a good read in regards to this.
As I used the "Auth" component I had to add in the function
beforeFilter()
of my controller this line:
$this->Auth->allow('anAction', 'anotherAction', '**login**');

Rails 4: Any way to fix/use regular html href links w/o 'link_to'?

So my question is that I have a link to 'pages/home' and I click on it, ill go to my home page.
But then I'll try to click again, but the link changes to 'pages/pages/home' and then I'll get a routing error. Is there anyway to fix this using regular old anchor tags? or do i need to use link_to?
edit:
This is how i insert my link into the page.
Home
This is not related to rails, the problem is you use a relative url :
Home
This will lead to <any_path_you're_in>/pages/home.
For it to be absolute, you have to use (note the leading slash):
Home
By the way, it's quite a bad practice to use hardcoded url to your own rails app. You can avoid using #link_to while still taking advantage of rails' routing :
Home
Provided you have a "home" route, of course :
get '/pages/home' => 'pages#home', as: 'home'
This will save you a lot of pain when you decide to restructure your app.

Routing errors while processing URL(s) without a http prefix

We render HTML within a certain page and certain links don't have a http prefix (e.g. foo.com/bar) when you click on it throws a routing error. Is there a easier way to navigate to the right URL in such cases
Normally, the link_to takes care of you in this regard. You say html, so I assume we can't use ERB code, so you'll have to hardcode the links into the HTML.
Make sense?
Of course without any html code or routes.rb code, I can't tell you whether or not there isn't anything wrong with your existing code.
Why do you need to use absolute links within your application? Navigating to just "/bar" on that case would be fine.
If you use the link_to helper function on your views, it should rely on url_for which by default generates relative paths.
If you need absolute paths, you can specify a default host on your application (How do I set default host for url helpers in rails?) or specify it on the host option for the url_for function (http://apidock.com/rails/v3.2.13/ActionView/Helpers/UrlHelper/url_for).

External link routes in rails

Simple enough. When were making a typical restful rails app we would keep all routes inside of our applicaiton. Very rarely would a path link to an external path. But if we were to do it, I'm wondering what the best way is.
A typical matching of home
match "home"=>"appcontroller#home"
If we were matching an external url to a variable of path. We might do something like the below?
First method
Routes.rb
match "external"=>"http:/www.google.ie"
Then in our html.erb
<%= link_to 'Google', external_path %>
Note this is not actually a legal way of doing things but something similar may exist. It seems very close to the current way of defining paths in rails but with an external landing.
Second method
Something that I've seen done elsewhere is to create a global variable for the external URL and use it in the link. EG.
in environment.rb or production.rb or whatever
#ext_path="http:/www.google.ie"
Then in our html.erb
<%= link_to 'Google', #ext_path %>
So to recap. Whats the best way to use external URLS in rails. Paths? Variables? Other?
Any input appreciated
I would have kept external links only in views. Because this links are not related to the any kind of logic of the application, and it's just an UI elements.
So, this way seems to me the best:
<%= link_to 'Google', "http://google.ie" %>
If you need to use this element many times, maybe it makes sense to bring this code into the helper, for example:
def search_engine_link
link_to 'Google', "http://google.ie"
end
And I really think that is's not very good place to introduce a more complex logic.
I'd probably use application-wide helpers wrapped around constants.
Helpers because I'd rather not see constants in templates, constants for environment-specific values.
I might use a hash to store the URLs: this keeps them more-tightly-coupled, and would allow environment-wide defaults, overriding per-environment as necessary.
The helpers could take symbols, or be generated from the hash keys, to generate xxx_path methods as happens with routes in routes.rb.
I found myself needing to do this because I had a link to an external site which I intended to make local in future.
In Rails 4 you can add this to config/routes.rb:
get '/contact',
to: redirect('http://www.example.com/contact.php'),
as: 'contact_us'
Which at the price of an extra redirect, lets you write:
<%= link_to "Contact us", contact_us_path %>
301-redirects
A note about use of redirect(...) in the routes file. Redirect generates a 301-redirect, which is a specific thing, Google describes it as...
If you need to change the URL of a page as it is shown in search
engine results, we recommend that you use a server-side 301 redirect.
This is the best way to ensure that users and search engines are
directed to the correct page. The 301 status code means that a page
has permanently moved to a new location.
So, using a 301-redirect in your routes for links to external websites may work, which it does, but the status information that is carried with that redirect is not correct.
If you've moved pages in your web app and want to tell the Google index (and everyone else) about it, then by all means use the redirect() to assure Google updates the index, but it's not optimal to use for standard external links out of your web app.

Adding content to static files(pages)

I have several static files(pages), which are basically copies of my website pages source code, with the content changed.
These files support my website, (keeping the same format) in various ways.
For example the menu part is:-
<body>
<div id="menu">
<ul class="level1" id="root">
etc
etc. until
</ul>
</div>
Unfortunately every month or so my menu bar changes and I have to update each static file manually.
As each of my static files have the same menu.
Is it possible to have one menu file which can be updated and have the static files load them automatically.
I plan to have several more static files. So this would be a great help if someone can suggest how to accomplish this.
Oh yes. Use some javascript magic to load the menu bar upon page load and keep it in menu.html.
One solution may be to use a spider (wget --recursive) to download generated pages directly from your application. One command, and you have the full copy of your site. (just add some useful options, like --convert-links, for example).
The other option may be to write an after_filter in your controller, and write the generated content to a file (not always, but for example when you add a parameter ?refresh_copy=1). Maybe just turning on page caching would be suitable? But the problem will be that you will not be able to trigger the controller action so easily.
If you don't want the whole site copied, just add some specific routes or controllers (/mirrorable/...) and run the spider on them, or just access them manually (to trigger saving the content in the files).
I ended up creating one controller without a model.
rails g controller staticpages
I then created a layout file which imported the individual changes to the layout, via a "yield" tied to a "content_for" in the view files(static files(pages) in the "view of staticpages" (for example abbreviations, aboutthissite etc etc).
The rest of the static file loaded with the usual "yield" in the layout. Works a treat. No more updating the menu bar all done automatically.
To get to the correct static file I created a route using:-
match 'static/:static_page_name'=> 'staticpages#show' (or in rails 2.x:-
map.connect 'static/:static_page_name', :controller=> "staticpages", :action=> "show"
"static_page_name" variable accepted anything after "/static/" in the url and passed it to the controller "staticpages" in which I set up a show action containing:-
def show
#static_page_name = params[:static_page_name]
allowed_pages = %w(abbreviations aboutthissite etc, etc,)
if allowed_pages.include?(#static_page_name)
render #static_page_name
else
redirect_to '/' #redirects to homepage if link does not exists
end
end
I then only had to change the links in the website. (e.g.<%= link_to " About This Site ", '/static/aboutthissite' %>)
and viola! its all working.

Resources