show.html.erb页面供current_user以外的用户查看

时间:2015-03-03 11:04:27

标签: html ruby-on-rails ruby

我正在尝试创建另一个用户可以查看的user/show.html.erb页面,该页面呈现所有已查看用户的信息。这包括称为汽车的物体的饲料。我无法正确渲染Feed。

用户/ show.html.erb

<div>
  <aside class="col-md-4">
    <div>
      <h1>My Profile</h1>
    </div>
    <div class="hero-unit user_info">
      <h1><%= @user.name %></h1>
      <h2><%= pluralize(@user.cars.count, "Car") %></h2>
    </div>
  </aside>

  <div class="col-md-8">
    <h1>My Cars</h1>
  </div>
  <div class="col-md-8">
    <div id="car_feed">
        <ul class="cars">
          <%= render @feed_items %>  //problem point
        </ul>
</div>
  </div>
</div>

渲染@feed_items部分给了我麻烦。 Feed在user.rb

中定义
class User < ActiveRecord::Base
  has_many :cars
  attr_accessor :remember_token
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :phone, presence:true
  validates :password, length: { minimum: 6 }, allow_blank: true

.
.
.

  def feed
    Car.where("user_id = ?", id)
  end
end

并在静态页面控制器中输入项目

class StaticPagesController&lt; ApplicationController中

  def home
    if logged_in?
      @car = current_user.cars.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end
end

有两类用户,我需要其他类“商店”才能在show.html.erb页面上查看此页面以及Feed中属于该用户的所有对象。

我没有收到错误消息,但只是文字代码在页面上被删除了。我该怎么做?

enter image description here

users_controller.rb

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

    def index
      @users = User.all
    end

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

    def new
      @user = User.new
    end

    def create
      @user = User.new(user_params)
      if @user.save
        log_in @user
        flash[:success] = "Thank you for signing up, welcome to ensage!"
        redirect_to root_path
      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)
        flash[:success] = "Profile updated"
        redirect_to @user
      else
          render 'edit'
      end
    end

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

    private

    def user_params
      params.require(:user).permit(:name, :email, :phone, :password,
                                   :password_confirmation)
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

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

2 个答案:

答案 0 :(得分:0)

你需要这样的东西:

用户/ show.html.erb

<ul class="cars">
    <% @feed_itmes.each do |feed_items| %>
         <li><%= feed_items %></li>  
    <% end %>
</ul>

另外,将您的@feed_items变量放在用户控制器中的show方法中:

def show
  @feed_items = current_user.feed.paginate(page: params[:page])
end

答案 1 :(得分:0)

由于您在上次评论中提供的信息,问题就更明确了。要使当前代码生效,请尝试在UsersController#show中添加该行:

@feed_items = @user.feed.paginate(page: params[:page])

所以你的行动如下:

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

希望它让你的模板有效!