2015-09-07 99 views
1

返回HTML黑我在我的模型的方法被稱爲在一個模式是這樣的:導軌 - 從模型

<div class="col-sm-9"><%= @notification.notifier_context %></div> 

在這種方法中,我要回一些HTML這樣的,其中有一個導軌幫手路徑在裏面。

context = "<td><%= link_to('link', account_item_path(@item.account, @item))%></td>".html_safe 

但它只是輸出字符串。我如何才能將它評估爲HTML正確?

感謝

回答

0

嘗試:

"<td>#{link_to('link', Rails.application.routes.url_helpers.account_item_path(@item.account, @item))}</td>".html_safe 

字符串不是ERB模板,所以你不能使用<%=%>語法。 對此,使用助手(與content_tag)或partials也是一個好主意。 另外,如果您需要使用模型類中的路線,請閱讀following answer

+0

這個答案效果很好 - 希望它是實現這個目標的合適方法! – user3437721

+0

@ user3437721最好的辦法是使用單獨的部分。通常模型不用於顯示視圖。 – tiktak

+0

它目前是_form.html.erb部分,它調用模型 – user3437721