Unified Ruby on Rails Route

I have a self-join through the intersection table in the application I'm working on, as shown below.

class User < ActiveRecord::Base
  has_many :clients, :through => :client_relationships, :conditions => "approved='1'", :source=>:user
end

It works in the sense that I can say @ current_user.clients and get all clients. However, I would like to set up URLs / clients where I can list all current clients clients. Does anyone know how I can do this?

I tried to configure the resource: clients on my routes and the client controller, but since there is no client model, this also causes errors.

+3
source share
1 answer

There should be no mistakes.

# routes.rb
map.resources :clients

# clients_controller.rb
class ClientsController < ApplicationController
  def index
    @clients = @current_user.clients
  end

  # other actions...
end  

# clients index.html.erb
<% @clients.each do |c| %>
  <p><%= c.name %></p>
<% end %>
0
source

Source: https://habr.com/ru/post/1756096/


All Articles