在我的网站上,一个用户有一个个人档案,其中有一个指向他个人外部网站的链接.我在postgresql数据库中以名称website存储的站点的url.当我测试结果时,我总是得到这样一个url:

http://localhost:3000/www.example.com

而不是http://www.example.com

我的视图index.html.erb如下所示:

<% provide(:title, 'All projects') %>
<h1>All projects</h1>

<%= will_paginate %>

<ul class="microposts">
    <%= render @microposts %>
</ul>

<%= will_paginate %>

我的_micropost.html.erb岁生日是这样的:

<li>
    <span class="title"><%= micropost.title %></span>
    <span class="website"><%= link_to micropost.website, micropost.website %></span>
    <span class="content"><%= micropost.content %></span>
    <span class="timestamp">
        Posted <%= time_ago_in_words(micropost.created_at) %> ago.
    </span>
</li>

我不知道这个案子有什么问题.如果我把@设置在micropost.website之前,它会给我一个错误undefined method `website' for nil:NilClass

有人能帮我吗(我是RoR初学者)?

费边

推荐答案

听起来你存储的URL没有http://,所以它们被解释为相对URL.你只需要这样做:

link_to micropost.website, "http://#{micropost.website}"

或者,在模型中添加一个full_url方法,如果缺少,可以添加它.

顺便说一下,你不能在那部分中使用@micropost,因为它不存在(你只有@micropostsmicropost).

Ruby-on-rails相关问答推荐

在Ruby on rails中,重复访问ActiveRecordModel返回相同的对象:预期行为还是错误?

Turbo-Rails:如何将 Turbo Frame 添加到 `application.html.erb`?

计算总价(商品数量*商品价格)

在 Rails 7 中,可以将导入映射与 js Bundle 解决方案一起使用,还是它们是专有的?

$redis 全局变量与 ruby​​ on rails

Ruby - ActiveRecord::ConnectionNotEstablished

跳过模型中的某些验证方法

Ruby on Rails 的 varchar 迁移问题

Rails 和 OS X:如何安装 rmagick?

使用 Devise 令牌登录,这是内置的吗?

具有条件的列的计数器缓存?

水豚 click_link 与 :href 匹配

如何将变量传递给render_to_string?

如何显示 RSpec 测试生成的 SQL 查询日志(log)?

Rails:将所有未知路由重定向到 root_url

在rails中带有内部文本或html的link_to image_tag

您将如何在 RoR 中创建类似 SO 或 Facebook 的通知系统?

Test::Unit Rails - 如何断言一个数字大于另一个?

反编译开发assets资源 管道

在 Rails 中显示主机名和数据库名