Do even simple pages need routes in Rails? - ruby

I wanted to make a simple page for a footer of a site discussing the team of the project. Such a page usually just has information and nothing too fancy.
I didn't make a route for it, and just basically saved it as team.html.rb in the folder: /app/views/team.html.rb
I am not sure whether that is the right place for such a file to be saved to. Where is the best place for such a one-off file?
Also, do I still need to make a route and a controller for this, or can I just skip those for such a simple page?
Thanks!!

I'm using a controller to deliver static pages.
Here's a link to a similar question:
How to do static content in Rails?
What you can do also is put a static html file into the 'public' folder and refer to it like this:
http://localhost:3000/name-of-static-file

I don't think that will work because Rails expects all requests to go through a controller. The simplest thing would be to do something like this at the end of your routes.rb:
match "/:action" => "pages"
Then in app/views/pages you can put your team.html.erb. You also will probably need at least a blank controller in app/views/pages_controller.rb:
class PagesController < ApplicationController
end
The nice thing about this is if you happen to need some dynamic content for one of the "static" pages you can just define a controller method to load it.
If this seems like overkill for your site, you might want a lighter framework like Sinatra

routes point to controller actions, not views.
a request to /team would (iirc) point to application#team. If that action doesn't exist, you will get an error. If you DO have that action defined (with a call to render, it will look in /app/views/application for a file called team.html[.erb|.json|.etc]
As the related thread points out, thoughtbot's high_voltage gem is great for a base of serving static pages.

Related

How to differentiate between two dynamic url in Laravel

I have two dynamic url with simillar structure. For example, lets say, Product page and category page.
I have set both pages in
Route::get('/{product}', [UsersController:: class, 'productDetail']);
Route::get('/{category}', [UsersController:: class, 'categoryProducts']);
But when I click on url which suppose to go in category page, it redirect to product page only because of same structure. How I can differentiate both URLs for Laravel without altering their url structure?
I don't think this can be done without modifying the URL pattern at least a little bit.
If you do something like /50?type=category then in the show method you can use the query parameter to determine which table to look at. But you'll have to use the same show method and I don't recommend doing it this way.
I hope someone else will be able to shine some more light on the matter.
this is the best practice for your case to make yourapi Resful
Route::get('/product/{product-id}', [UsersController:: class, 'productDetail']);
Route::get('/product/categories, [UsersController:: class, 'categoryProducts']);
learn more about Restful api here https://restfulapi.net/resource-naming/
This should be done by calling index, update diff() function. You can try by using the below:
Route::get('/category/{slug}', 'site\categorycontroller#show')->name('category.show');
Route::get('/product/{slug}', 'site\productcontroller#show')->name('product.show');

How to load the layout at runtime in Magento?

I know that we can design the layout in *.xml then in the action just invoke loadLayout, and renderLayout to render the blocks/views.
But, I have a question is:
- How can I load the layout at runtime?
If we have an action which does not really design its layout and will be decided how to render at runtime.
You can please consider the answer from the question for more clear.
Writing a new answer because it seems that you actually DO still want to render, you just want to render a different route's layout XML updates. I believe the _forward() method from Mage_Core_Controller_Varien_Action will allow you to do what you are describing with the least amount of pain.
You should add your action controller directory ahead of the catalog directory, create a ProductController with a viewAction, and check customer is not logged in - in this check you would call $this->_forward('customer','account','login');.
This approach though is going to require more effort in order to be usable, as I imagine that you want the user to be sent to the product page upon login. Have you seen Vinai Kopp's Login Only Catalog module? It should do this for you.
loadLayout() and renderLayout() just execute block output method toHtml() (usually) and take the resulting strings and apply them to the response object via appendBody(). In an action controller you can just call $this->getResponse()->setBody('response string'). How you build the string is up to you.
You can also use Mage_Core_Block_Flush to immediately send output to the browser without using the response object.

Is There A Downside To Calling Models From Helpers In CakePHP?

A bit of context: I need to cache the homepage of my CakePHP site - apart from one small part, which displays events local to the user based on their IP address.
You can obviously use the <cake:nocache> tag to dictate a part of the page that shouldn't be cached; but you can't surround a controller-set variable with these tags to make it dynamic. Once a page is cached, that's it for the controller action, as far as I know.
What you can usefully surround with the nocache tags are elements and helpers. As such, I've created an element inside these tags, which calls a helper function to access the model and get the appropriate data. To get at the model from the helper I'm using:
$this->Modelname =& ClassRegistry::init("Modelname");
This seems to me, however, to be a kind of iffy way of doing things, both in terms of CakePHP and general MVC principles. So my question is, is this an appropriate way of getting what I want to do done, or should it ring warning bells? Is there a much better way of achieving my objectives that I'm just missing here?
Rather than using a Helper, try to put your code in an element and use requestAction inside of the element.
see this link
http://bakery.cakephp.org/articles/gwoo/2007/04/12/creating-reusable-elements-with-requestaction
This would be a much better approach than trying to use a model in your helper.
Other than breaking all the carefully-laid principles of MVC?
In addition to putting this item into an element, why not fetch it with a trivial bit of ajax?
Put the call in its own controller action, such that the destination URL -> /controller/action (quite convenient!)
Pass the IP back to that action for use in the find call
Set the ajax update callback to target within the element with the results of the call accordingly
No need to muck around calling Models directly from Views, and no need to bog things down with requestAction. :)
HTH

Using and hiding default class

This is my first time getting my hands dirty with CI so I'm getting a little confused.
I'm wanting to accomplish a couple things with my question. First of all, I'd like to always use the default controller without having it to appear in the url. For example, I created a new class named after my site (Example.php) and that works fine. However, if I want to call the search function in my controller I then have to go to example.com/index.php/example/search/.
The second thing I want to accomplish is when I run a search I'll get a nice looking url like so: example.com/search/This+is+a+search (I haven't gotten to removing the index.php portion but I know to use a htaccess). I'm not worried about the actual mechanics of the search, just that I'd like to format the url in this way.
I originally experimented with using a Search class but that found that it doesn't allow me put the search in the url because the second parameter should be a function and not the extra stuff.
Thanks for any help.
In application/config/routes.php file add $route to redirect everything to your controller.
Something like this:
$route['([^\/]+)'] = 'content/index/$1';
$route['([^\/]+)\/([^\/]+)'] = 'content/index/$1/$2';
This will redirect urls like example.com/A and example.com/A/B to a controller named content. Parameters A and B will be passed to method index.

Getting the url of an aspx page using the page type

I'm using a web application project.
I have a folder in my web root called Users and in the folder I have a page called UserList.aspx
What I want to be able to do is type in Response.Redirect(Users.UserList.URL)
What I reckon I can probably do is create a class that extends Page and add a static property called URL that calls MethodInfo.GetCurrentMethod().ReflectedType (I think this works haven't tested) and then have that convert Users.UserList -> ~/Users/UserList.aspx
The problems with this method that I know of are one I need to go through every page and make it extend the base class and it doesn't work with any pages that contain a '-' character.
The advantages are that if pages are moved around then there aren't any broken links (Resharper gives out when there is a Page with the wrong namespace).
Also then every individual page that takes query string params could have a static method so that if I want to add/remove params I can see what uses those params etc.
Also if I want to call that page I don't have to check the name of the params e.g. UserId userId, Id or id. So that would look something like Users.ViewUser.GetUrl(1) -> ~/Users/ViewUser.aspx?UserId=1
So the question is: Is there a better way of doing this? Or is this a bad idea in principal?
You could just create an extension method for the base Page class that does what you are thinking. That would avoid having to go back and modify the base class for all your pages.
There is a better way. Create a traffic cop that knows about paths. Then if paths change, your data model changes or other stuff you just change that one place. Plus you could have read from a config file and make changes at run time.
Thus your call looks like this:
Repose.Redirect(TrafficCop["Users.UserList"].URL)
or some other way if you don't like the syntax.
The MethodInfo.GetCurrentMethod().ReflectedType doesn't work so I came up with another method of doing this using generics.
Instead of Users.ViewUser.GetUrl() or Users.ViewUser.URL it's GetUrl()
For a page with parameters it's still Users.ViewUser.GetUrl(1), it isn't ideal because they should both have the same way of being called but better than strings I guess.
Going to leave the question open for a while just in case.
edidt: I think I will actually just create another method called GetUrl(String getQuery) because if I have two parameters that are of the same type it doesn't work very well.
further edit: I found out how to do exactly what I want to do.
created a class called BasePage:Page where T : Page
on that are the static methods redirect and geturl
each page inherits from the base page as follows: MyPage:BasePage
Any page can redirect to that page by using the command MyPage.Redirect();

Resources