摆脱其他用户的供稿项

时间:2019-01-25 09:19:15

标签: ruby-on-rails ruby activerecord

我已经按照Hartl的教程制作了带有标记系统的ToDoList,并且还借助了word guidevideo。 直到今天,我仍然好奇Hartl在Listing 13.3中实现的供稿系统的代码如何工作。对于我当前的应用程序,在实现标签之前,使用较简单的版本可以使提要在微博所有者的主页上保持可见。

仅显示自己帖子的正确Feed视图 Wrong after implementing tags 以及每次我尝试首次登录时的日志。

  Rendered users/show.html.erb within layouts/application (36.7ms)
  Rendered layouts/_rails_default.html.erb (24.4ms)
  Rendered layouts/_shim.html.erb (0.3ms)
  Rendered layouts/_header.html.erb (0.7ms)
  Rendered layouts/_footer.html.erb (1.0ms)

但是,现在实施标签会导致显示所有用户的供稿。

显示错误的版本

correct but pre-tagged

这是我第一次进入标签系统的登录页面时在命令行上的日志。

  Rendered collection of microposts/_micropost.html.erb [30 times] (80.7ms)
  CACHE  (0.0ms)  SELECT COUNT(*) FROM "microposts"
  Rendered shared/_feed.html.erb (86.8ms)
  Rendered static_pages/home.html.erb within layouts/application (138.2ms)
  Rendered layouts/_rails_default.html.erb (63.7ms)
  Rendered layouts/_shim.html.erb (0.3ms)
  Rendered layouts/_header.html.erb (1.3ms)
  Rendered layouts/_footer.html.erb (1.0ms)

这是我的控制器代码:

MicropostsController

class MicropostsController < ApplicationController
  before_action :logged_in_user, only: [:index, :show, :create, :destroy]
  before_action :correct_user,   only: :destroy


  def index
    params[:tag] ? @microposts = Micropost.tagged_with(params[:tag]) : @microposts = Micropost.all
  end


  def show
    @micropost = Micropost.find(params[:id])
  end



  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_url
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  def destroy
    @micropost.destroy
    flash[:success] = "You have deleted a task!"
    redirect_to request.referrer || root_url
  end

  private

    def micropost_params
      params.require(:micropost).permit(:content, :tag_list, :tag, 
        {tag_ids: [] }, :tag_ids)
    end

    def correct_user
      @micropost = current_user.microposts.find_by(id: params[:id])
      redirect_to root_url if @micropost.nil?
    end
end

SessionsController

class SessionsController < ApplicationController

  def new
  end

  def create
    @user = User.find_by(email: params[:session][:email].downcase)
    if (@user && @user.authenticate(params[:session][:password]))
      log_in @user
      flash[:success] = "Welcome back, #{@user.name}!"
      params[:session][:remember_me] == '1' ? remember(@user) : forget(@user)
      redirect_back_or root_path
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end
end

StaticPagesController

class StaticPagesController < ApplicationController
  def home
    if logged_in?
      @new_micropost = Micropost.new
      @micropost  = current_user.microposts
      @feed_items = Micropost.all.paginate(page: params[:page])

    end 
  end

  def help
  end

  def about
  end

  def contact
  end
end

UsersController

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 show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

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


  def create
   @user = User.new(user_params)
   if @user.save
     log_in @user
     flash[:info] = "Welcome to the to-do app, #{@user.name}"
     redirect_to @user
   else
     render 'new'
   end
  end

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

  def destroy
    a = User.find(params[:id]).name
    User.find(params[:id]).destroy
    flash[:success] = "#{a} has been deleted!"
    redirect_to users_url
  end

  def admin_user
    redirect_to(root_url) unless current_user.admin?
  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 for @user.name has been updated"
    redirect_to(@user)
  else
    flash[:danger] = "Update Failed."
    render 'edit'
  end
end

  private
    def user_params
      params.require(:user).permit(:name, :email, :password,
                                  :password_confirmation, :admin)
    end
    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "You are not logged in. Please log in."
        redirect_to login_url
      end
    end

    def correct_user
      @user = User.find(params[:id])
      if !current_user?(@user)
        flash[:danger] = "You are not authorized to visit this page."
        redirect_to(root_url)
      end
    end
  end

microposts / index.html.erb

<h1>Filtered Micropost Page</h1>
<div class = "col-md-8 offset-2">
  <% @microposts.each do |micropost| %>
    <p><%= truncate(micropost.content, length: 50) %></p>
    <p><small>Tags: <%= raw micropost.tags.map(&:name).map { |t| link_to t, tag_path(t) }.join(', ') %></small</p>
        <span class="timestamp">
            Posted <%= time_ago_in_words(micropost.created_at) %> ago.
            <% if current_user?(micropost.user) %>
              <%= link_to "Done", micropost_path(micropost), method: :delete, data: { confirm: "Keep up the good work!" } %>
            <% end %>
        </span>
    <% end %>
</div>

有人知道如何解决问题吗?如果需要更多信息,请告诉我。

2 个答案:

答案 0 :(得分:2)

从您共享的代码来看,我认为您的问题出在StaticPagesControllerhome动作中。在那里,您可以按以下方式检索@feed_items

  @new_micropost = Micropost.new
  @micropost  = current_user.microposts
  @feed_items = Micropost.all.paginate(page: params[:page])

,我想您正在Feed中显示@feed_items吗?如果是这样,则设计使您可以显示所有可能用户的所有可能微博。

所以我猜应该是

  @feed_items = current_user.microposts.paginate(page: params[:page]) 

(不确定是否需要/使用@micropost变量,因为这似乎是多余的/重叠的) (也不知道这与标签有什么关系,因为在您显示的代码中我看不到标签的引用)

答案 1 :(得分:0)

您需要在MicropostsController#index中向查询添加用户条件,例如通过使用current_user.microposts关系

def index
  @microposts = current_user.microposts
  @microposts = @microposts.tagged_with(params[:tag]) if params[:tag]
end

在显示和更新页面上,类似范围可能会很有用-否则用户将能够查看和更新​​属于其他用户的内容。