Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Nikola Novakovic
Nikola Novakovic
4,171 Points

Ruby On Rails Symbols

Hello there Treehouse! :)

I am a programmer who is trying to learn Ruby , in that last couple of months. When I worked on web projects before , most of the backend was done in php ( some form of it, be it a framework, wordpress etc ) and I am honestly having my head blown out sometimes to grasp some of the core concepts of Ruby and Ruby On Rails.( I also worked on stuff like android java, js frameworks, so that also helps me to grasp mvc model of course but not some points below. I know that there is a concept of routing in for example Angular , but I am not totally grasping this in Rails) I am really really liking the syntax of the language, how readable and sometimes logical it is and I love learning new stuff that is why I want to stick with it and that is why I am asking this question.

I am currently doing "Odot" Ruby On Rails course and I learned before on Symbols and what they do , but sometimes I really do get confused since double dots are flying everywhere and I can not quite catch why are they there. I know that Ruby can declare symbols in different ways and that they are used in hashes ( those ways include :example "Value" or example: "Value" or the one with the hashrocket ) but I am not totally getting this:

todo_list_id: params[:todo_list_id]

or

<%= link_to "Delete", todo_list_todo_item_path(todo_item), method: :delete, data: {confirm: "Are you sure?"} %>

or

patch :complete

or

render action: :edit

or

redirect_to todo_list_todo_items_path, notice: "Todo item marked as complete."

Ok so let me start from the last example here. How I see this is that redirect to is a method and everything else are params of that method. Now I get sorta confused on the second param where we have a symbol notice: and then a string. Does this mean that notice has a value of that string ? I am pretty sure that this is the case, but I got so confused lately with all of this that I just have to ask.

The render action: :edit is weird. I get that render is a method and that it takes a param called action:. I looked at the docs and that is where I saw that this :edit is actually a symbol that is refferncing to a view and it can be actually written as a string and actually action: param is totally optional. That really blew my socks off. So if anyone can explain this into a little bit more detail, what is a param, does that mean that :edit is a value of action or what is actually going on here ?

Patch :complete is actually pretty straight forward I think , but just wanted to double check without having to go to stack overflow and flying through posts ( and this is related to my confusion general with symbols so I would probably not find the appropriate answer ). So patch is a method that takes an argument and that argument is a symbol, right ? Now it is intresting how some methods can actually take either strings or symbols and ruby converts them to symbols.

The one with <%= %> is also readeable and I can sorta see what is going on. We have a link to , that links to "Delete, then a path that is there provided by routes and then there comes again weird thing method: :delete. What does that mean? A method: is an actual method that has a symbol name instead of a name as regular name ? Does it mean that :delete is an argument of method: ? And then the last part data: which I have similar questions as for method delete, but the difference is that it accepts a hash and not a single symbol, but the same questions apply.

The first sample , I also have similar questions as for a previous one. todo_list_id: is what in this case ? A symbol name of a method instead of a name or what? and then the params part is an argument of todo_list_id method or is that a method at all? and then we have a [:todo_list_id] is that a hash with an empty value or ?

Wow it took me awhile to write this out actually. And I really do realize that this is a loooooooooooooooooooooong text but it would really mean a world to me if Jason Seifer would help out, or anyone else who rocks in Rails.

I am really being nerdy and sorta boring all around the forum with all of these Rails questions , and I am really sorry, but this is the only way to truly understand some stuff that are not clear in my head, ask :)

Hope you have a good one everyone! :)

2 Answers

Does this mean that notice has a value of that string?

Short answer: yes. Long answer:

   redirect_to todo_list_todo_items_path, notice: "Todo item marked as complete."

So in this case we're taking advantage of the fact that ruby can accept a hash as the last parameter of a method without surrounding it in { } brackets. A more explicit version of this method call would look like this:

  redirect_to( todo_list_todo_items_path(), {notice: "Todo item marked as complete."} )

This makes it a little more clear what's going on here - the second parameter to redirect_to is a hash with the key :notice and the value "Todo item marked as complete.".

The render action: :edit is weird. I get that render is a method and that it takes a param called action:

Not quite. This is similar to what is happening in the redirect_to call; render is accepting a hash who's only key is the symbol :action with the value of :edit, another symbol. You are correct in that edit can be either a string or a symbol here. This is however a common means for adding in named parameters when defining a ruby method.

So patch is a method that takes an argument and that argument is a symbol, right?

Spot on.

Now it is intresting how some methods can actually take either strings or symbols and ruby converts them to symbols.

This is actually more of Rails' doing than Ruby. While Ruby is a dynamically typed language so there's nothing stopping you from passing "taco" in as a parameter to Float#round, it will in most cases raise an exception. Case in point because i wanted to use .round('taco') in an example:

>> 1.234567.round('taco')
TypeError: no implicit conversion of String into Integer
    from (irb):2:in `round'
    from (irb):2
    from /Users/gparsons/.rvm/rubies/ruby-2.1.4/bin/irb:11:in `<main>'

That said Rails adds a couple of nice features for us such as HashWithIndifferentAccess. These hashes allow us to reference keys by either a string or a symbol:

>> h = HashWithIndifferentAccess.new()
=> {}
>> h[:tacos] = "Good"
=> "Good"
>> h["cabbage"] = "Not so good"
=> "Not so good"
>> h
=> {"tacos"=>"Good", "cabbage"=>"Not so good"}
>> h[:cabbage]
=> "Not so good"
>> h['tacos']
=> "Good"

... then there comes again weird thing method: :delete. What does that mean?

This is another case where rewriting the method call may prove helpful:

<%= link_to( "Delete", todo_list_todo_item_path(todo_item), { method: :delete, data: {confirm: "Are you sure?"} } ) %>

In this case we've got a nested hash being sent as the last parameter to link_to. So what you would end up with is the following hash:

{
   method: :delete,
   data: {
      confirm: "Are you sure?"
   }
}

Hope that helps a little. Let me know if you have any questions!

Nikola Novakovic
Nikola Novakovic
4,171 Points

Wow this really is an in depth answer. Thank you so much for that. When you actually typed methods in a more strict way it made so much more sense, I am used to that syntax.

Just one question can an actual name of the ruby ( or ruby on rails ) method be a symbol ? I probably got it confused with something, because it does not make huge sense in my head, but this is ruby and I wanted to ask. Also I have a question on rails routes , but I think that will be another question :)

Just one question can an actual name of the ruby ( or ruby on rails ) method be a symbol ?

Not really. A symbol is a variable type much like String or Float. You can call methods via a symbol of their name using the send method but that's not really what you were asking i don't think.