Do I need a controller for any routes in Rails? - html

I am implementing an html template for a Contact us page in my app. I am wondering if I can somehow pass a link for that page, which will have no behaviour, without having to generate a new controller which seems to be not possible. I tried to place the template in the public folder and then pass a link to /public/contact, being the template contact.html.erb, but rails returns a uninitialised controller routing error.
here is what I have set in the routes.rb file
get 'public/contact'
and this is the application.html.erb that carries my link to that
</div>
<ul>
<li>HOME</li>
<li>NEWS</li>
<li>FAQ</li>
<li>CONTACT US</li>
</ul>
Notice that we have also the path info/news and info/faq
I had generated a controller info to handle all these static pages. I would just like to know if this a good practice or should I use other drier way to implement this?

It is actually a good approach to have a controller for your static pages as well, you can actually generate a controller for pages to handle the static pages like about, contact e.t.c rails generate controller pages contact about
you would just access that in
localhost:3000/pages/contact
Then your view would be in app/views/pages/contact.html.erb
routes.rb
get "pages/contact"
get "pages/about"

wHY do you need to generate a new controller just for a request.Recommended way is to just add a new action in your common/utility controller with a get request to show about us.
for example: match '/about' => 'pages#about'
This is good as you can cache the page to boost performance as well you can make it dynamic(html.erb)
OR
Assuming that the static file(only html) is in public folder,ADD an anchor tag directly to open it in new page using target=_blank attribute.
<%= link_to "About Us", "/about_us.html" ,:target=>"_blank"%>

Related

How to have multiple HTML static pages on a ASP.NET MVC project using the Layout page?

On my MVC project I have to incorporate 40 static pages.
I want these pages to use the Layout page.
What is the best way to do that?
I know this question was asked before but I didn't find any good answer.
Any advise?
I don't relly know ASP, but I try to give a generic answer.
So I think if you have a lot of similar static pages, somehow you could make a controller action that handles all these pages. For example the action gets the name of the page as a path variable in the URL, and return the view according to that.
But if that is not possible in the language you are using, you can just make simple separate actions for these pages. Maybe you could group the related ones into the same controller, so you would have a few controllers that handle these pages, and they are not stuffed in one controller.
Basically the solution is very simple, you have to create views for you static HTML (cshtml), then you should add a Route to your Route.Config like this:
routes.MapRoute(
"OrdeForm",
"OrderForm/{file}",
new { controller = "MyController", action = "Page", file} = "" }
);
Where "File" is a dynamic parameter that gets the View name from the URL and renders the right View.
The global controller should be something like this:
public class OrderFormController : Controller
{
public ActionResult Index(string file)
{
return View(file);
}
}
That works perfectly!
Thank you #Erik Philips for the excellant answer!

web2py controller for layout.html

I'm building a web application using web2py, and have currently implemented search on some of the view/ pages by creating a function in the appropriate controller and then using {{=form}} to render it in my HTML page like this...
<h3>Search</h3>
{{=form}}
{{if results:}}
<h3>Results</h3>
{{for result in results:}}
{{=A(result.title)}}
{{pass}}
Rather than repeat this functionality in multiple pages, I would like to have a search field in my layout.html file, which can then be used across all over views. However, there is no controller for layout.html. Is it possible to use a method in another controller in the layout.html page, or is there another way to link a controller to layout.html that I am missing?
Thanks.

how to link a page in spring mvc

Hello, I am working on spring mvc and I have in my home page a a simple link of page register and the register.jsp is contained in my WEB-INF/pages/register.jsp
So on my index.jsp which is in my base folder means in webcontent parllel to WEB-INF so how can i link this register.jsp
<li class="float-block" style="margin-left:50px;"><a href="pages/register.jsp" class="login" title="New User Register here" >Register</a></li>
Please suggest me how to do this to run this link
You can do that by redirecting it to a controller and then to the jsp of your need.
Check this link.
It depends on your configuration. Check request mapping in your controller class and all xml configurations.
Normally it should be something like <a href="/register"> depends on the controller.
a href tag name and request mapping of controller class address should be same and add the object of that class in Form Tag like

How can I configure my route.rb page to redictect to a view

In my route.rb file I currently have:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.root :controller => "test"
how do I direct my index page to direct to something like this:
http://site.com/railstest/test/index.html
or just:
http://site.com/railstest/test.erb
originally it started off at:
http://site.com/railstest/
which took me to the default html page, which has now been deleted. should I change the route or create a test.rb in the view folder, thank you
It's not possible to make a route that goes directly to a view, bypassing every controller.
So you have two options available to you now:
make it as a static html page
create the full html page
save it in /public/railstest/filename.html
use the URL: site.com/railstest/filename.html
note: hard to maintain, and cannot take advantage of layouts.
create a controller that will serve your view
either "script/generate RailstestController index" of "rails g PagesController index"
delete the app/views/index.html.erb that it creates and copy yourview file to that name instead
should Just Work

How to serve static pages with Zend Framework

We currently employ Zend Framework and the MVC pattern on our website. We have large numbers of static pages, which are in random areas of the site. These pages are not simple HTML pages that can bypass ZF altogether... they would participate in the Zend_Layout, etc.
What is the best way to serve these pages without creating a separate action/controller for each random page? Redoing the layout of the pages so that they all fall under a "misc" controller or something is not an option, the pages need to stay where they are in our URL hierarchy for SEO purposes.
If I understand the question properly:
You have a bunch of static content you would like to apply your layout to.
These pages of static content already have existing urls you don't want to break
Zend actually separates URL from $controller->action(), it just so happens the MVC part of Zend has a default setting to do this. You can still create a "misc" controller which receives any random url, you just need to define some custom routes.
http://framework.zend.com/manual/en/zend.controller.router.html
quoting example Zend Framework site:
$route = new Zend_Controller_Router_Route_Static(
'login',
array('controller' => 'auth', 'action' => 'login')
);
$router->addRoute('login', $route);
Above route will match a URL of
http://domain.com/login, and dispatch
to AuthController::loginAction().
More ambitious examples using pattern matching can be found on the same page.