在Rails中创建条件link_to语句?

Ada*_*ton 4 ruby-on-rails link-to

在下面的代码中,我试图这样做,如果用户已经接受邀请,他们将能够点击"不参加"div来拒绝邀请.

这一点逻辑工作正常,但我试图得到它,所以无论用户是否已接受邀请,"不参加"div出现.

现在,只有在用户接受了邀请时才会显示div.

有没有办法使link_to语句有条件,但保留div无论如何?(也就是说,使得div始终存在,但如果用户接受了邀请,它只是一个链接?)

<% if invite.accepted %>
    <%= link_to(:controller => "invites", :action => "not_attending") do %>                             
        <div class="not_attending_div">
             not attending
        </div>
    <% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

Hit*_*eeb 6

<%= link_to_if invite.accepted ... %>
Run Code Online (Sandbox Code Playgroud)

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if

编辑:

link_to_if使用link_to_unless哪些使用link_to的代码,它应该使用相同的选项相同

  def link_to_unless(condition, name, options = {}, html_options = {}, &block)
    if condition
      if block_given?
        block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options, &block)
      else
        name
      end
    else
      link_to(name, options, html_options)
    end
  end
Run Code Online (Sandbox Code Playgroud)

<%=
   link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
     link_to(@current_user.login, { :controller => "accounts", :action => "show", :id => @current_user })
   end
%>
Run Code Online (Sandbox Code Playgroud)

在这里查看http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_unless

编辑:

这是否达到了您的需求.对不起,因为没有更好地阅读这个问题.

<div class="not_attending_div">
   <%= link_to_if invite.accepted, "not attending", (:controller => "invites", :action => "not_attending") %>
</div>
Run Code Online (Sandbox Code Playgroud)