Difference between class = "with equal sign in front" and class: "with Colon"

I'm new to rubies on rails, sorry if this is a dumb question.

Here I have an example class = ""

class="table table-striped"

      

and below example class: ""

class: "btn btn-primary btn btn-success"

      

Please explain to me what is the difference between them. Thanks you

+3


source to share


3 answers


class="

is an HTML expression and class: "

is a Ruby expression.

You didn't have any context for where you see them, so let me insert my own.

Say you have a view index.html.erb

with the following markup:



<table class="table table-striped">
  <% @people.each do |person| %>
    <tr class="person">
      <td><%= person.first_name %></td>
      <td><%= person.last_name  %></td>
      <td><%= person.last_name  %></td>
      <td><%= link_to 'View', person, class: 'btn btn-default btn-sm' %></td>
    </tr>u
  <% end %>
</table>

      

Here you can see that the element table

has classes table table-striped

and they are defined in HTML using the first syntax.

On the other hand, the latter td

calls a helper link_to

defined by Rails and passes the argument class

as a Ruby hash. The documentation forlink_to

shows that the method takes a hash with a name html_options

where the argument ends class

.

+3


source


class="table table-striped"

: This type of syntax is displayed in HTML code. Example:<td class="right">Ticket Counts:</td>

class: "btn btn-primary btn btn-success"

: This type of syntax is used when it is a ruby ​​code. Example:<%= link_to 'Home', home_path, class: "home-link" %>



This generates the <a class="home-link" href="/" title="Home">Home</a>

html tag .

+1


source


The first one assigns String

literal to a "table table-striped"

local variable named class

(this is a bad idea since it class

is a reserved word in Ruby and you have to jump over the hoops to look up that variable using a method Binding#local_variable_get

).

The second is just illegal syntax and throws an exception as a result SyntaxError

.

Syntax like this is only possible

  • in literal Hash

    :

    { class: "btn btn-primary btn btn-success" }
    
          

    where it will create Hash

    with a key Symbol

    :class

    referencing the valueString

    "btn btn-primary btn btn-success"

  • in the parameter list:

    def foo(class: "btn btn-primary btn btn-success")
    
          

    which is an optional keyword parameter with a name class

    with a default argument "btn btn-primary btn btn-success"

    (which again is a bad idea because it class

    is a reserved word and you will need to use Binding#local_variable_get

    lookup inside the method body)

  • in the argument list:

    foo(class: "btn btn-primary btn btn-success")
    
          

    here it is passing keyword argument class

    with String

    literal "btn btn-primary btn btn-success"

    as message argument sendfoo

But it can never happen on its own, as you showed it.

0


source







All Articles