2016-09-24 71 views
0

我目前正在使用haml編寫一個RoR項目,並且我有一個我從未見過的問題。Ruby on Rails/HAML image_path無法在其他條件下工作?

我有一個「用戶配置文件」與一個可選的圖像。我想顯示圖像,如果它存在,如果沒有,我展示一個佔位符(存儲在資產=>「管理/ avatar.png」所以,這裏是我下面的示例代碼:

-unless @user.image.nil? 
    =image_tag @user.image.url,class:"img-responsive img-circle",style:"width:150px;height:150px;margin:auto;" 
-else 
    %img.img-responsive.img-circle{:src => image_path("admin/avatar.png"),style:"width:150px;height:150px;margin:auto;"} 
%img.img-responsive.img-circle{:src => image_path("admin/avatar.png"),style:"width:150px;height:150px;margin:auto;"} 

第一

%img.img-responsive.img-circle{:src => image_path("admin/avatar.png"),style:"width:150px;height:150px;margin:auto;"} 

不工作(並顯示 「失蹤」),並且是Seconde系列實際工作。我與這個有點失落。

,有人嗎?

感謝。

+0

你確定「失蹤」不是從'image_tag'? – Aleksey

+0

'@ user.image.url'等於什麼? – Aleksey

+0

@Maxime Bauer,你是否也可以發佈你的用戶模型代碼,因爲如果你的用戶模型中定義了默認url,那麼它總是會執行第一個塊而不是其他塊 –

回答

0

unless else是一種代碼味道

室溫下將我提出:

- if @user.image.present? 
= image_tag(@user.image.url, class:"img-responsive img-circle",style:"width:150px;height:150px;margin:auto;") 
- else 
= image_tag('admin/avatar', class:"img-responsive img-circle", style:"width:150px;height:150px;margin:auto;") 
+0

它穿着。順便說一句,我不知道爲什麼除非其他條件產生錯誤,當我想鏈接資產圖像。這很奇怪,謝謝:) –

相關問題