2016-04-21 13 views
0

我正在製作一個組織者網站,其中有人可以組織他們的客戶,項目等。但是我遇到了技術挑戰,我似乎不知道如何獲得圍繞(還)。RAILS:單獨的用戶在應該不同時顯示相同的內容在視圖中

我的目標

獨立用戶生活在自己的世界裏,他們創造他們自己的客戶顯示哪些客戶頁面上,當他們登錄並導航到用戶頁面。

目前面臨的挑戰

通過法克爾寶石播種單獨的用戶數據庫後,單獨的用戶目前可在索引視圖相同的客戶名單,當他們瀏覽到客戶端頁面,當我希望他們有上市的字符不同的字符串(代表不同的客戶端)

enter image description here

我已經試過

我目前正在修改RailsTutorial.org Chp11: Micropost Guide以適合我的客戶端場景,並嘗試在客戶端控制器中創建SHOW和INDEX函數,但在整個GET/POST和控制器操作場景中仍然相當模糊,這是我認爲的地方我擊中了技術塊。

我的代碼

的routes.rb

get 'password_resets/newedit' 

    root    'static_pages#home' 
    get 'about' => 'static_pages#about' 
    get 'signup' => 'users#new' 

    get 'login' => 'sessions#new' 
    #the page for new session 
    post 'login' => 'sessions#create' 
    #creates a new session 
    delete 'logout' =>'sessions#destroy' 
    #deletes the session 
    get 'clients' => 'clients#show' 

    resources :users 
    resources :account_activations, only: [:edit] 
    resources :password_resets,  only: [:new, :create, :edit, :update] 
end 

Clients.html.erb

<%= provide(:title, 'Clients') %> 

<div class="clients-container container"> 
    <div class="row"> 
     <!-- Add pagination later for multiple folders over multiple pages --> 
    <% if @user.clients.any? %> 
     <%= render @clients %> 
     <!-- render produces the following code: 
      from the partial file; 

      <div class="col-md-2 client-folder" style="margin: 10px" id="client - <%= client.id %>"> 
      <span class="clientName" ><%= client.client_name %></span> <br> 
      <span class="contactName"><%= client.contact_name %></span> 
      </div> 
     --> 

     <%= will_paginate @microposts %> 
    <% end %> 
    </div> 
</div> 

Clients_controller.rb

class ClientsController < ApplicationController 

    def show 
     @user = current_user 
     @clients = @user.clients.paginate(page: params[:page]) 
    end 
end 

User_controller.rb

class UsersController < ApplicationController 
    before_action :logged_in_user, only: [:index, :edit, :update, :destroy] 
    before_action :correct_user, only: [:edit, :update] #restricted areas 
    before_action :admin_user, only: :destroy 

    def index 
    @users = User.paginate(page: params[:page]) 
    end 

    def show 
    @user = User.find(params[:id]) 
    end 

    def new 
    @user = User.new 
    render :layout => 'signup.html.erb' 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     @user.send_activation_email 
     UserMailer.account_activation(@user).deliver_now 
     flash[:success] = "Welcome to focus. Please check your e-mail to activate your account." 
     redirect_to root_url 
    else 
     render 'new' 
    end 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(user_params) 
     #Handle a successful update. 
     flash[:success] = "Your information has been updated." 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    User.find(params[:id]).destroy # destroys users 
    flash[:success] = "Users deleted" 
    redirect_to users_url 
    end 
    private 

    #Confirms a logged-in user. 
    def logged_in_user 
     unless logged_in? 
     store_location 
     #While false... 
     flash[:danger] = "Please log in." 
     redirect_to login_url 
     end 
    end 

    #Confirms the correct user. 
    def correct_user 
     @user=User.find(params[:id]) 
     redirect_to(root_url) unless current_user?(@user) 
     #If another user gets access to one user's id# 
     #Rails gets the user profile id and tests it 
     #against the id of User logged in current 
    end  

    def user_params 
     # 'strong parameter' to prevent mass assignment vulnerability 
     # user_params privatize to limits external exposure 
     params.require(:user).permit(:name,:email, 
            :password,:password_confirmation) 
    end 

    # Confirms admin user 
    def admin_user 
     redirect_to(root_url) unless current_user.admin? 
    end 
end 

部分部分:seed.db

users = User.order(:created_at).take(3) 
50.times do 
    client_name = Faker::Lorem.characters(10) 
    contact_name = Faker::Lorem.characters(10) 
    contact_email = Faker::Lorem.characters(10) 
    contact_phone = Faker::Lorem.characters(10) 
    client_address = Faker::Lorem.characters(10) 
    users.each { |user| user.clients.create!(client_name: client_name, 
              contact_name: contact_name, 
              contact_email: contact_email, 
              contact_phone: contact_phone, 
              client_address: client_address) } 
    end 

如果你會這麼好心地指出什麼我沒有看到從技術上講,這將是非常感謝!

編輯;我希望更多的圖片可以幫助: enter image description here

編輯2: 裝上最後的工作代碼的人誰可能會發現有用感謝@GoGoCarl的輸入:

users = User.order(:created_at).take(3) 
50.times do 
    users.each { |user| 
    client_name = Faker::Lorem.characters(10) 
    contact_name = Faker::Lorem.characters(10) 
    contact_email = Faker::Lorem.characters(10) 
    contact_phone = Faker::Lorem.characters(10) 
    client_address = Faker::Lorem.characters(10) 

    user.clients.create!(client_name: client_name, 
              contact_name: contact_name, 
              contact_email: contact_email, 
              contact_phone: contact_phone, 
              client_address: client_address) } 

    end 

回答

2

你播種循環需要反轉。您正在創建50個客戶端,並且對於每個組,您迭代用戶並添加客戶端。這意味着每個用戶將獲得同一組客戶端。由ID不同,但外觀相同(字段名稱)。三個用戶中的每一個都得到具有給定預設屬性(名稱,電子郵件等)的客戶端

而是,反轉循環;首先對每個用戶進行迭代,然後爲每個用戶創建50個客戶端,並將這50個客戶端添加到該用戶。

現在,所有的客戶端應該是彼此不同的,而不是共享相同的字段。

注:這個循環也將工作,如果你沒有指定變量,只是做:


user.clients.create!(client_name: Faker::Lorem.characters(10), 
    contact_name: Faker::Lorem.characters(10), 
    contact_email: Faker::Lorem.characters(10), 
    contact_phone: Faker::Lorem.characters(10), 
    client_address: Faker::Lorem.characters(10)) 

然後,每次創建被調用時,將代替生成一組新的隨機字符。不過,我認爲反轉循環會使代碼更加明顯。但是,這都是相對的!

相關問題