在下面的代码中,我试图做到这一点,如果用户接受了邀请,他们将能够点击“不参加”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 %>
最佳答案
<%= link_to_if invite.accepted ... %>
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
例子
<%=
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
%>
在这里查看 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>
关于ruby-on-rails - 在 Rails 中制作条件 link_to 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11233532/