How add logic to Views? Ruby on Rails

Posted by Gotjosh on Stack Overflow See other posts from Stack Overflow or by Gotjosh
Published on 2010-06-11T16:12:47Z Indexed on 2010/06/11 16:32 UTC
Read the original article Hit count: 201

Filed under:
|
|
|
|

Right now I'm building a project management app in rails, here is some background info:

Right now i have 2 models, one is User and the other one is Client. Clients and Users have a one-to-one relationship (client -> has_one and user -> belongs_to which means that the foreign key it's in the users table)

So what I'm trying to do it's once you add a client you can actually add credentials (add an user) to that client, in order to do so all the clients are being displayed with a link next to that client's name meaning that you can actually create credentials for that client.

What i can't figure it out how to do is, that if you actually have credentials in the database (meaning that there's a record in the users table with your client id) then don't display that link.

Here's what i thought that would work

<% for client in @client%>
     <h5>
         <h4><%= client.id %></h4>
         <a href="/clients/<%= client.id %>"><%= client.name %></a>
          <% for user in @user %>
            <% if user.client_id = client.id %>
                <a href="/clients/<%= client.id %>/user/new">Credentials</a>
            <%end%>
          <% end %>
     </h5>
<% end %> 

And here's the controller:

def index
@client = Client.find_all_by_admin(0)
@user = User.find(:all)
end

but instead it just puts the link the amount of times per records in the user table. Any help?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby