5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

link_to (Haml)タグにクラス名をつける

Posted at

syntax error, unexpected keyword_ensure, expecting end-of-input

Haml in Rails:画像をリンクにするときのエラー:

間違えたコード:

= link_to %a{:href => "file:///Users/HOkaniwa/Desktop/IBplat%202/Questions%23index_home.html"}
  = image_tag "logo2.png"

エラー:SyntaxError in QuestionsController#index

直したコード:

%li= link_to image_tag('logo2.png', id:"logo"), questions_path

画像を使わない他の例:

%li= link_to '質問する', questions_path, class: "a btn btn-default" 

直すこと
1:パス名を確認して直す。

(NameError in Questions#indexを直す) http://localhost:3000/rails/info/routes をみて、questions からquestions_path として正しいパスを書く。 この場合はindexアクションだったため、以下; = link_to 'image_tag "logo2.png"', questions_path

エラー:このままだとイメージタグが文字としてページに表示される。
ためしに' 'をなくしてみると = link_to image_tag "logo2.png", questions_path となり、エラーが出る。
undefined method `symbolize_keys' for "/questions":String

2: image_tag('image名') とする。

  = link_to image_tag('logo2.png'), questions_path

3: HTML inputを Haml text_fieldにする
エラー:Syntax error

=form_for :question, url: questions_path do |f|
%p
= f.label :質問タイトル
= f.text_field :question_title 'text_field',:class => "input form-control"
%p
= f.label :質問内容
= f.text_area :question_content
%p
= f.submit
-#もとのコード:
%input.form-control{:placeholder => "質問のタイトル", :type => "text"}
 
-#直したコード:
            = f.text_field :question_title, :class => "input form-control",:placeholder => "質問のタイトル", :type => "text"
5
8
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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?