LoginSignup
14
12

More than 3 years have passed since last update.

【Rails】link_toメソッドでaria-haspopup、data-toggleなどのHTML表現を入れる方法

Posted at

エラー発生

Railsのlinks_toメソッドで下記のようにaria-haspopup、aria-expanded、data-toggleなどのHTML表現を入れようとしたところ、エラーが出た。

<%= link_to root_path, class: 'nav-link', data-toggle: dropdown, aria-haspopup: true, aria-expanded: false, id: header-profile do %>

  <%= image_tag('xxx.jpg', size: '60x60') %>

<% end %> 

syntax error, unexpected 〇〇, expecting 〇〇
というエラーが出まくる。
構文エラーなのでどこか書き間違えがあるのかと思ったが、コードを見ても余計な文字や入れ忘れがあるように見えない。

解決方法

調べて見るとlink_toの引数のハッシュとしてつける場合、キーをシングルクオテーションをつける必要がある。例えば、data-toggle: dropdown'data-toggle': :dropdownという風に書き換える。

<%= link_to root_path, class: 'nav-link', 'data-toggle': :dropdown, 'aria-haspopup': :'true', 'aria-expanded': :'false', 'id': :'header-profile' do %>

  <%= image_tag('xxx.jpg', size: '60x60') %>

<% end %> 

参照
https://teratail.com/questions/124849

14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12