One of the best practices is keeping your controllers with as little code as possible, and have all the validations in the models. Rails includes a lot of validations that you can use in your models, and many other helpers that help you focus on your business logic and develop your projects as quickly as possible. A helper method that is among the unpopular ones is the possibility to check the old value of an attribute of a object, before doing any database transaction.
Tag: rails (Page 2 of 3)
Globalize is one of the most famous gems that is used for translating dynamic content in Rails application. It is really easy to use, and brings a lot of conveniences immediately after you add it in your application. I recently had to do a more complex query using the table added by Globalize generator, and I could not find that much information about ways of doing joins for this newly added table with the one that it corresponds and that translates its attributes. In this article, you can see a short example on how to do that.
There can be cases where you wished you named that controller a lot better, as it does not make any sense and is very ambiguous in its current form. If you have ever wondered whether there is a way for you to do quick renamings of multiple related files inside a Rails project, then I have some good news for you. There is already a gem called Rails Refactor, that can be used to do these renamings. Continue reading
We are in an interconnected world, where people across the planet can use the projects that we develop, and having the opportunity to offer them with additional international languages make them a lot better. We can obviously use locale for doing the translations of static strings like labels, or placeholder descriptions that appear across our applications, so we should also have something that can give us a way to translate the dynamic content. There is a really great Ruby gem that makes the translation of model attributes a lot easier. This gem is called Globalize and is very easy to use.
I recently had an issue with Rails Admin, which was displaying duplicate models in the left hand side menu. It was something that I had not encountered before, and it turned out, it was a rare occasion, as I could not find that much coverage about it. I also saw that this unanswered question related to this problem was published more than a year and a half ago in StackOverflow. This is the reason why I decided to write this post. Continue reading
It is highly likely that you will have to work on a project that has some type of search forms implemented. Fortunately, as with many other type of functionalities that you can easily get out of the box without writing too much code in Rails projects, there is also a gem that is specific for this purpose. Ransack is a Ruby gem that enables the creation of both simple and advanced search forms for Ruby on Rails projects.
It’s obvious that a lot of applications need an admin panel, where the administrator or other lower level users need privileges to do certain data manipulations. Not everyone of them should be a technical person. Thus, you may need to provide them a user-friendly interface which gives them this opportunity. Fortunately, you can insert an admin panel in your Rails application easily using RailsAdmin.
It is no doubt that there are certain pages that you do not need to have them viewed by all the visitors of your application. You can also have users of different roles that may not be allowed to access certain parts of your applications. There is a really good Ruby gem called cancan that excels at this. It makes the authorization of your app resources pretty straight-forward, so you do not have to duplicate permission checking code across controllers and views.
Rails controllers have callbacks that can be useful when we want to execute a particular piece of code before or after other methods have been invoked. However, there is no Rails built-in after_response_sent callback that could be used to invoke a method after we have rendered the response to the user, either via an HTML or JSON response. Continue reading
Developers may feel they do not have that much creativity when it comes to use random words as arguments, or values for many fields. For example, if we have some test cases when we are creating a new instance of a Book object, chances are that we will assign ‘book’ to this object’s title, which does not seem that much interesting. We code because it is something that gives us pleasure, and getting help with these types of difficulties can be helpful. Fortunately, there is a Ruby gem called Faker that generates random fake value that you can use in your tests.