RAILS:在视图中显示相同内容的单独用户应该是不同的

时间:2016-04-21 02:34:37

标签: ruby-on-rails ruby-on-rails-4 railstutorial.org

我正在建立一个组织者网站,有人可以组织他们的客户,项目等。但是我遇到了技术挑战,我似乎不知道怎么回事(还)。

我的目标

单独的用户居住在他们自己的世界中,他们在那里创建自己的客户端,这些客户端在登录后显示在客户端页面上并导航到客户端页面。

当前的挑战

通过Faker gem为单独的用户播放数据库后,当他们导航到客户端页面时,单独的用户当前在索引视图中显示相同的客户端列表,当我希望他们列出不同的字符串时(表示不同的客户)

enter image description here

我尝试了什么

我目前正在调整RailsTutorial.org Chp11: Micropost Guide以适应我的客户端场景,并尝试在客户端控制器中创建SHOW和INDEX函数,但在整个GET / POST和控制器操作场景中仍然相当模糊,这是在哪里,我认为我正在进入技术块。

我的代码

的routes.rb

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="xsd:assert"/>
</xsl:stylesheet>

Clients.html.erb

  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_controller.rb

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

User_controller.rb

class ClientsController < ApplicationController

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

部分:seed.db

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

如果你非常友好地指出我在技术上没有看到的东西,那将非常感激!

编辑;我希望其他图片可以提供帮助: enter image description here

编辑2: 通过@GoGoCarl的输入为任何可能找到有用的人附加最终工作代码:

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

1 个答案:

答案 0 :(得分: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))

然后,每次调用create时,都会生成一组新的随机字符。不过,我认为反转循环会使代码更加明显。但是,那都是相对的!