Secure routes with constraints in Ruby on Rails

In Ruby on Rails, one of the most important aspects of web development is ensuring that your routes are secure. One way to do this is to use constraints on the parameters passed through your routes. In this blog post, we will explore what constraints are, how they work, and how to use them to secure your Rails routes.

What are constraints in Rails routes?

Constraints are a way to limit the parameters that are passed through your Rails routes. By default, Rails routes will accept any parameters that are passed to them, which can be a security risk if you’re not careful. Constraints allow you to specify certain conditions that a parameter must meet in order for the route to be valid.

For example, you might want to restrict a parameter to only accept numbers, or to only accept values within a certain range. You can use constraints to enforce these conditions and make sure that your routes are secure.

How do constraints work in Rails routes?

Constraints are defined using regular expressions (regex). When a parameter is passed through a route, Rails checks the parameter against the constraint using the regex. If the parameter matches the regex, the route is considered valid and the request is processed. If the parameter does not match the regex, the route is considered invalid and the request is rejected.

Here’s an example:

Rails.application.routes.draw do
  get '/products/:id', to: 'products#show', constraints: { id: /\d+/ }
end

In this example, we have a route that accepts a parameter called “id”. We’ve added a constraint to this route that requires the “id” parameter to be a digit (\d+). This means that the route will only be valid if the “id” parameter contains one or more digits.

How to use constraints in Rails routes?

To use constraints in your Rails routes, you need to specify them in the route definition. You can do this by adding a “constraints” option to the route, followed by a hash of constraints. The keys in the hash should match the names of the parameters in the route, and the values should be regexes that define the constraints.

Here are a few examples:

Rails.application.routes.draw do
  # Only accept numeric values for the "id" parameter
  get '/products/:id', to: 'products#show', constraints: { id: /\d+/ }

  # Only accept values that match the "name" parameter format
  get '/products/:name', to: 'products#show', constraints: { name: /[a-z]+/ }

  # Only accept values within a certain range for the "quantity" parameter
  get '/products/:quantity', to: 'products#show', constraints: { quantity: /[1-9][0-9]?/ }
end

In the first example, we’re only accepting numeric values for the “id” parameter. In the second example, we’re only accepting values that match the format of lowercase letters for the “name” parameter. In the third example, we’re only accepting values within the range of 1-99 for the “quantity” parameter.

By using constraints, you can ensure that your routes only accept parameters that meet certain criteria, making your web application more secure and less vulnerable to attacks.

Conclusion

Constraints are a powerful tool in Ruby on Rails that allow you to secure your routes by restricting the parameters that are passed through them. By using regular expressions to define constraints, you can enforce conditions on the parameters and make sure that your routes are only accepting valid input. With constraints, you can improve the security and reliability of your Rails web application.

Read More...

Operator method call in Ruby

In Ruby, you can call methods on objects using the dot notation. However, there is an alternative way to call methods on objects, which is called the operator method call. In this blog post, we will explore what operator method call is, how it works, and when you might want to use it.

What is operator method call?

In Ruby, many operators such as “+”, “-“, “*”, and “/” are actually method calls on objects. For example, when you add two numbers with the + operator, you are actually calling the “+” method on the left-hand side operand, passing the right-hand side operand as an argument. Operator method call is a way to call methods using the operator syntax, instead of the dot notation. For example, instead of writing “a + b”, you can write “a.+(b)”.

How does it work?

To use operator method call, you need to define the corresponding method for the operator. For example, if you want to use the “+” operator to add two objects of your custom class, you need to define the “+” method for that class.

Here’s an example:

class MyClas
  attr_accessor :value

  def initialize(value)
    @value = value
  end

  def +(other)
    MyClass.new(@value + other.value)
  end
end

a = MyClass.new(10)
b = MyClass.new(20)
c = a + b # calls the "+" method on a, passing b as an argument
puts c.value # prints 30

In this example, we define a custom class MyClass with a “+” method that adds the values of two MyClass objects and returns a new MyClass object with the result.

When we create two MyClass objects “a” and “b” with values 10 and 20, respectively, we can use the “+” operator to add them together. This calls the “+” method on “a” with “b” as an argument, creating a new MyClass object “c” with the value of 30.

When should you use operator method call?

Operator method call is not used very often in Ruby, as the dot notation is the more common way to call methods. However, there are a few cases where operator method call can be useful.

One example is when defining mathematical operations on custom classes, as we saw in the previous example. Another example is when defining comparison operators such as “<” and “>”. For example:

class MyClas
  attr_accessor :value

  def initialize(value)
    @value = value
  end

  def <(other)
    @value < other.value
  end
end

a = MyClass.new(10)
b = MyClass.new(20)
puts a < b # calls the "<" method on a, passing b as an argument

In this example, we define a custom “<” method for MyClass that compares the value of two MyClass objects. We can then use the “<” operator to compare two objects of MyClass.

Conclusion

Operator method call is a way to call methods on objects using the operator syntax, instead of the dot notation. It is not used very often in Ruby, but can be useful when defining mathematical or comparison operations on custom classes.

Read More...

Active Admin - Custom Pages, Views and Actions

Active Admin is a Ruby on Rails plugin to generate Admin console for the application. It has a lots of functionalities implemented out of the box where we just need to integrate it and use it. Most of the time you don’t need to customize the plugin.

But the main Advantage of Active Admin is that you can customize almost all the built-in functionalities. The main functionalities are: Navigation, User Authentication, Scopes, Action Items, Index styles, Filters, Sidebar sections, API and Downloads.

By default Active Admin will generate the pages to CRUD a model when you generate the resource file for the model using rails generator:

    $> rails generate active_admin:resource [MyModelName]
  

Read More...

Integrating FriendlyID, Inherited Reource and Active Admin

For my latest spare time project Kazhakkoottam I had to integrate friendly id with the app. But the application was already integrated and running with Inherited Resource and Active Admin. So while integrating with friendly id I have faced some issues.

So why we need to use friendly id?
FriendlyId is used to implement pretty URL's, so that it will be SEO friendly and can be identified easily. Instead of id's you can configure name or title field as the slug. For example http://www.kazhakkoottam.com/items/krishna-theatre is the friendly id for http://www.kazhakkoottam.com/items/1.

Read More...

Active Admin tips and tricks

For my current project I had to setup Admin console. I used Active Admin for that purpose. It is very easy to setup an Admin console. And they have a very good documentation. But if you want make some customization you have to spent some time digging for solution. Following are some customization I made to improve the Admin console:

1.member_label

If you are using select box for showing association in form page or filters, it will show the object string if there is no attribute 'name' for that model. By default the select box will take the name attribute of the model as the values. So you have to use member_label to specify the attribute you want to show in the select box.
f.input :user, :as => :select, :member_label => :email
  
Read More...