我是Rails noob,我正在关注我发现here...
的博客文章我一切都在努力直到最后。事情变得模糊不清。
所以,如果我在帮手中有这个......
module ApplicationHelper
def display_content_with_links(tweet)
tweet.content.gsub(/(http:\/\/[a-zA-Z0-9\/\.\+\-_:?&=]+)/) {|a| "<a href=\"#{a}\">#{a}</a>"}
end
end
我不应该在我的视图中显示我的推文...
我做错了什么?
答案 0 :(得分:3)
您将需要一个控制器和视图才能显示此信息。简单的事情:
# app/controller/tweets_controller.rb
TweetsController < ApplicationController
def index
@tweets = Tweet.get_latest
end
end
并在视图中:
# app/views/tweets/index.html.haml
%ul
- @tweets.each do |tweet|
%li
= display_content_with_links tweet
或者如果你使用erb
# app/views/tweets/index.html.erb
<ul>
<% @tweets.each do |tweet| %>
<li>
<%= display_content_with_links tweet %>
</li>
<% end %>
</ul>
这是一个非常基本的例子,甚至可能没有达到你想要的,但它应该指向正确的方向。
答案 1 :(得分:0)
实际上,您应该添加
@tweet = Tweet.all
到已经设置的控制器#动作,然后在视图中迭代它们:
<ul>
<% @tweets.each do |tweet| %>
<li><%= display_content_with_links tweet %></li>
<% end %>
</ul>