6

In the code below, I'm trying to make it so that, if a user has accepted an invite, they'll be able to click on the "not attending" div to decline the invite.

That bit of logic works fine, but I'm trying to get it so the "not attending" div shows up regardless of whether the user has accepted the invite.

Right now, the div only appears if the user has accepted the invite.

Is there a way to make the link_to statement conditional, but preserve the div regardless? (That is, make it so the div is always present, but is only a link if the user's accepted the invite?)

<% if invite.accepted %>
    <%= link_to(:controller => "invites", :action => "not_attending") do %>                             
        <div class="not_attending_div">
             not attending
        </div>
    <% end %>
<% end %>
Adam Templeton
  • 4,467
  • 7
  • 27
  • 39

2 Answers2

8
<%= link_to_if invite.accepted ... %>

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

Edit:

link_to_if uses link_to_unless which uses link_to 's code, it should work the same with the same options

  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

Example

<%=
   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
%>

check it here http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_unless

Edit:

Does this achieve what you need. Sorry, for not reading the question better.

<div class="not_attending_div">
   <%= link_to_if invite.accepted, "not attending", (:controller => "invites", :action => "not_attending") %>
</div>
Sully
  • 14,672
  • 5
  • 54
  • 79
  • This might do it. I'll need to play around a bit first. The main problem right now is getting it to accept the div as the link name. – Adam Templeton Jun 27 '12 at 19:32
  • Unfortunately, I can't get it take the not_attending div as the name argument. And the API makes it seems as though the link_to_if method doesn't have as many options. – Adam Templeton Jun 27 '12 at 19:50
  • I did look at the link_to_unless API as well, actually. But, I'm still not seeing a way to replace the required "name" parameter with a div. If I try and leave it out, it just turns the options into plain text and uses it as a link name. And if I try to include the actual div html as the name parameter, it just blows up. – Adam Templeton Jun 27 '12 at 20:34
  • I still couldn't get it to work, but I do appreciate your help. I've got to move on, but I may come back to this when I've got time. Thanks again! – Adam Templeton Jun 27 '12 at 20:46
  • It is my pleasure. I apologize I read the question quickly as I was working on a bug. – Sully Jun 27 '12 at 22:22
1

Just answered here: How to create a link_to_if with block only if condition is met?

If you would want to show the block anyways, but only add the link if a certain condition is met, you may capture the block altogether and use it in a simple conditional:

<% block_content = capture do %>
  <div class="not_attending_div">
    not attending
  </div>
<% end %>

<% if invite.accepted %>
  <%= link_to block_content, controller: :invites, action: :not_attending %>
<% else %>
  <%= block_content %>
<% end %>
estani
  • 24,254
  • 2
  • 93
  • 76