简体   繁体   English

Rails:链接到外部属性URL

[英]Rails: link to an external attribute url

I'm new on Ror, I created a meetings index page, on my app, where it is possible to book a seat for a meeting. 我是Ror上的新手,我在应用程序上创建了一个会议索引页面,可以在其中预定会议的席位。 I would like to create for each meeting on this page, a button which redirects in target_blank to an external url. 我想为该页面上的每个会议创建一个按钮,该按钮将target_blank重定向到一个外部URL。 The external url is a string attribute (reservation) of my table. 外部网址是我的表格的字符串属性(保留)。 I don't find how to realize this feature. 我找不到如何实现此功能。 Thk in advance for your help. 提前感谢您的帮助。 Here is my code: 这是我的代码:

schema.db: schema.db:

 create_table "meetings", force: :cascade do |t|
    t.string "name"
    t.datetime "start_time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.string "details"
    t.string "address"
    t.boolean "highlight"
    t.string "reservation"
    t.string "participation"
    t.index ["user_id"], name: "index_meetings_on_user_id"
  end

meetings.html.erb: Meetings.html.erb:

<% @meetings.each do |meeting| %>
  <strong><%= meeting.name %></strong>
  <br><%= meeting.details %><hr>
  <% if user_signed_in? %>
  <%= link_to "Book a seat", ??????, :target => "_blank" %>
<% end %>
<% end %>

meetings/_form.html.erb: 会议/_form.html.erb:

<%= simple_form_for(@meeting) do |f| %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :start_time %>
<%= f.input :details %>
<%= f.input :address %>
<%= f.input :reservation %>
<%= f.input :participation %>
<%= f.check_box :highlight %> Mettre en avant cet évènement
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>

您可以像这样将URL传递给link_to帮助器:

<%= link_to "Book a seat", meeting.reservation, :target => "_blank" %>

For external links, you need a link with http (for example: http://stackoverflow.com ): 对于外部链接,您需要具有http的链接(例如:http: //stackoverflow.com ):

<%= link_to "your label", "your link with http", :target => "_blank" %>

So it should be 所以应该

<%= link_to "Book a seat", meeting.reservation, :target => "_blank" %>

This will create a link that opens in a new tab. 这将创建一个在新选项卡中打开的链接。

If you are not sure about your links starting with http you can use: 如果您不确定以http开头的链接,可以使用:

<%= link_to "Book a seat", meeting.reservation.starts_with?('http') ? meeting.reservation : "http://#{meeting.reservation}", :target => "_blank" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM