| Start |
|---|
Basic REST Controller |
| Output JSON instead of HTML In addition you will need the route: This will respond in two different ways to requests on
You can omit |
| Controllers (Basic) This is a basic controller, with the addition of the following route (in routes.rb): Will display the |
| Parameters Controllers have access to HTTP parameters (you might know them as As usual our route: Access the URL |
| Filtering parameters (Basic) You can allow (or reject) some params so that only what you want will pass through and you won't have bad surprises like user setting options not meant to be changed. Visiting |
| Redirecting Assuming the route: You can redirect to a different URL using: You can go back to the previous page the user visited using: Note that in Rails 5 the syntax for redirecting back is different: Which will try to redirect to the previous page and in case not possible (the browser is blocking the HTTP_REFERRER header), it will redirect to |
| Using Views Assuming the route: And the controller: The view The output will be a webpage with the text: "Hello World" If you want to render a different view, you can use: And the file You can pass variables to views using controller instance variables: And in the file And the output will be: "Hello john" An important note around the render syntax, you can omit the Can be written instead as: Rails is smart enough to figure out that it must render the file |
| Display error pages for exceptions If you want to display to your users meaningful errors instead of simple "sorry, something went wrong", Rails has a nice utility for the purpose. Open the file We can now add a It's recommended not to rescue from |
| Filters Filters are methods that are run "before", "after" or "around" a controller action. They are inherited, so if you set any in your Before Filter Before filters are executed before the controller action and can halt the request (and/or redirect). A common use is to verify if a user is logged in: Before filters are run on requests before the request gets to the controller’s action. It can return a response itself and completely bypass the action. Other common uses of before filters is validating a user’s authentication before granting them access to the action designated to handle their request. I’ve also seen them used to load a resource from the database, check permissions on a resource, or manage redirects under other circumstances. After Filter After filters are similar to "before" ones, but as they get executed after the action run they have access the response object that's about to be sent. So in short after filters are run after the action completes. It can modify the response. Most of the time if something is done in an after filter, it can be done in the action itself, but if there is some logic to be run after running any of a set of actions, then an after filter is a good place to do it. Generally, I’ve seen after and around filters used for logging. Around Filter Around filters may have logic before and after the action being run. It simply yields to the action in whatever place is necessary. Note that it doesn’t need to yield to the action and may run without doing so like a before filter. Around filters are responsible for running their associated actions by yielding, similar to how Rack middlewares work. Around callbacks wrap the execution of actions. You can write an around callback in two different styles. In the first, the callback is a single chunk of code. That code is called before the action is executed. If the callback code invokes yield, the action is executed. When the action completes, the callback code continues executing. Thus, the code before the yield is like a before action callback and the code after the yield is the after action callback. If the callback code never invokes yield. the action is not run-this is the same as having a before action callback return false. Here's an example of the around filter: This will catch exception of any action and put the message in your log. You can use around filters for exception handling, setup and teardown, and a myriad of other cases. Only and Except All filters can be applied to specific actions, using Skipping Filter All filters (inherited ones too) can also be skipped for some specific actions: As they're inherited, filters can also be defined in a Beware that in Rails 4.x you could use |
| Generating a controller Rails provides a lot of generators, for controllers too of course. You can generate a new controller by running this command in your app folder Note: You can also use For example, to generate a controller for a This will create the controller in It will also create a Furthermore, if you specified any actions they will also be added to your Rails creates a helper file for you, in At last, but not least, Rails also generates test files for your controller, your helper and your views. If you don't want any of these to be created for you can tell Rails to skip them, just prepend any option with
And the generator will skip both If you need to create a controller for a specific This will create your controller inside Rails can also generate a complete RESTful controller for you: |
| Rescuing ActiveRecord::RecordNotFound with redirect_to You can rescue a RecordNotFound exception with a redirect instead of showing an error page: |
| 404 when record not found Rescue from record not found error instead of showing an exception or white page: |
Read Article
https://develop.horje.com/learn/1434/reference