<%= params[:action] %>
and
<%= params['action'] %>
display
index
but what is the difference between this syntax?
What is the difference between a symbol and a string? A string, in Ruby, is a mutable series of characters or bytes. Symbols, on the other hand, are immutable values. Just like the integer 2 is a value.
Symbols are immutable; Strings aren't. Being immutable means that the values won't change. Therefore, Symbols are faster than Strings.
While params appears to be a hash, it is actually an instance of the ActionController::Parameters class.
We can check if a particular hash contains a particular key by using the method has_key?(key) . It returns true or false depending on whether the key exists in the hash or not.
In Rails, the params
hash is actually a HashWithIndifferentAccess
rather than a standard ruby Hash
object. This allows you to use either strings like 'action'
or symbols like :action
to access the contents.
You will get the same results regardless of what you use, but keep in mind this only works on HashWithIndifferentAccess
objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With