【问题标题】:How to "link_to" full path in rails 4?如何在 rails 4 中“link_to”完整路径?
【发布时间】:2015-08-16 21:00:18
【问题描述】:

这是我的代码

<a <%= link_to "open your box", gig_path(@gig), class: "mcnButton", target: "_blank", style: "font-weight: bold;letter-spacing: 0px;line-height: 100%;text-align: center;text-decoration: none;color: #FFFFFF;"%></a>

来自以上&lt;%= link_to "open your box", gig_path(@gig)

注意 gig_path(@gig) 它给了我 url http://gigs/3,它运行良好,它找到了我需要的 gig id:3 问题是它没有像这样提供完整的 url

http://example.com/gigs/3 附:作为参考,我可以做@gig.title,@gig.description 并且没有问题。

【问题讨论】:

    标签: html ruby-on-rails ruby ruby-on-rails-4 model-view-controller


    【解决方案1】:

    要获取完整的 URL,请使用 gig_url(@gig) 而不是 gig_path(@gig)

    【讨论】:

    • 是的,它确实在生产中工作,我刚刚检查过......但这很奇怪,而不是转到我的自定义域名 www.example.com/gigs/3 它转到我的 www.herokuapp /gigs/3 ,但在右边。为什么会这样?
    • 这是因为 Rails 无法可靠地自行确定您的主机名是什么。因此,您需要通过设置默认值来告诉它您要使用什么。详情请见stackoverflow.com/questions/2660172/…
    • 问题是我在 production.rb 中有一个 'config.action_mailer.default_url_options' 被重定向到 heroku url)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    相关资源
    最近更新 更多