逃脫我有這樣HTML中的Rails 3
<%= Navigation.with(params) do |menu|
if current_user && current_user.can_verify?
menu.item("Listings", manage_listings_path())
menu.item("Listing changes", needing_change_approval_manage_listings_path())
menu.item("Flagged Items", flagged_manage_listings_path())
menu.item("Transfers", manage_listing_transfers_path())
menu.item("Reviews", manage_listing_reviews_path())
end
if current_user && current_user.admin?
menu.item("Log", manage_verifications_path())
menu.item("Indexer Compensations", manage_compensations_path())
menu.item("Users", manage_users_path())
end
end%>
在我看來,一個方法調用打出下面的字符串
"<li><a href="/manage/listings" class="active">Listings</a></li> <li><a href="/manage/listings/needing_change_approval">Listing changes</a></li> <li><a href="/manage/listings/flagged">Flagged Items</a></li> <li><a href="/manage/listing_transfers">Transfers</a></li> <li><a href="/manage/listing_reviews">Reviews</a></li> <li><a href="/manage/verifications">Log</a></li> <li><a href="/manage/compensations">Indexer Compensations</a></li> <li><a href="/manage/users">Users</a></li>"
我剛剛得到這個串在我的網頁。我希望他們成爲CSS風格很好的菜單。我只是在我的頁面上獲得上述原始文本。如何將此字符串轉換爲瀏覽器的HTML對待。
請幫
這裏是導航類
class NavigationMenu < ActionView::Base
def initialize(params)
@params = params
end
def item(title, path, options={})
@items ||= Array.new
unless (route = Rails.application.routes.recognize_path(path,:method => options[:method]|| :get))
raise "Unrecognised path #{path}, are you sure it's in routes.rb?"
end
@items << content_tag(:li, link_to(title,path, :class => (@params[:controller] == route[:controller] && @params[:action] == route[:action])? 'active' : nil))
end
def output
return '' if @items.blank?
content_tag(:ul, @items.join("\n"), :id => 'navigation')
end
end
class Navigation
def self.with(params, &block)
menu = NavigationMenu.new(params)
yield menu
menu.output
end
end
我把方法調用放入raw()中...該字符串仍然不被視爲html – Anand
「Navigation.with」方法可能不會真正返回字符串,而是直接將其寫入輸出。在這種情況下,只有「Navigation」的原始開發人員可以提供幫助。 –
或者你正在冒險進入'Navigation'的內部。帶上一把砍刀。 – Matchu