ruby (学习rails教程时)如何在与用户控制器无关的页面上显示用户信息

kuhbmx9i  于 2023-02-21  发布在  Ruby
关注(0)|答案(1)|浏览(146)

抱歉问得含糊不清。另外,我不习惯英语,但我会尽我所能清楚地沟通。
现在是设置服务器并访问该页面的时候了。

ActiveRecord::RecordNotFound in StaticPagesController#diary
Couldn't find User without an ID
Extracted source (around line #9):
7
8   def diary
9    @user = User.find(params[:id])
     @micropost = current_user.microposts.build if logged_in?
    end
   end
10

解释一下这个错误。我在静态页面上添加了一个新页面,试图把用户的信息放在那里。我想添加一个日记发布功能。
文件的结构如图所示。the structure of the file
换句话说,我想在日记页面上显示用户信息,但我想我得到了一个错误,即找不到用户的ID。
我将把错误发生的日志发布页面的信息。

app/views/stataic_pages/diary.html.erb

<% if logged_in? %>
  <div class="row">
    <aside class="col-md-4">
      <section class="user_info">
        <%= render 'shared/user_info' %>
      </section>
      <section class="micropost_form">
        <%= render 'shared/micropost_form' %>
      </section>
    </aside>
  </div>
<% end %>

部分
一个二个一个一个
这是控制器

app/controllers/static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def diary
    @user = User.find(params[:id])
    @micropost = current_user.microposts.build if logged_in?
  end
end

路线

app/config/route.rb

Rails.application.routes.draw do
  get 'sessions/new'
  get 'users/new'
  root "static_pages#home"
  get '/help', to: "static_pages#help"
  get '/search', to: 'searchs#search'
  get '/signup', to: 'users#new'
  get    "/login",   to: "sessions#new"
  post   "/login",   to: "sessions#create"
  delete "/logout",  to: "sessions#destroy"
  get '/diary',  to: "static_pages#diary"
  resources :users
  resources :microposts,          only: [:create, :destroy]
end

我在Rails教程中学到的是,在登录后显示专用show页面的过程中,将此添加到用户控制器中,

app/controllers/user

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      reset_session
      log_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new', status: :unprocessable_entity
    end
  end

  private

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

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

   
    def admin_user
      redirect_to(root_url, status: :see_other) unless current_user.admin?
    end
end

它是在视图页面中编写类似这样的代码。

app/views/users/show.html.erb

<% provide(:title, @user.name) %>
<h1>
  <%= gravatar_for @user %>
  <%= @user.name %>
</h1>
<div class="col-md-8">
    <% if @user.microposts.any? %>
      <h3>Microposts (<%= @user.microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>
app/models/user.rb

class User < ApplicationRecord
    has_many :microposts, dependent: :destroy
    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: true
    has_secure_password
    validates :password, presence: true, length: { minimum: 6 }
end

我把这段代码放在了日记方法中,就像在这个用户的显示页面上显示配置文件一样。
@user =用户.查找(参数[:id])@microposts =@user. microposts.分页(页面:参数[:页])
但是我得到了上面的错误,是不是模型或者DB关系有问题,是不是有办法把用户信息带到和用户控制器无关的页面上,感谢大家阅读到这里,有问题请留言。

kmbjn2e3

kmbjn2e31#

真正的问题是你如何查找资源。
@user = User.find(params[:id])意味着用户id是URI的一部分,并且由于您的路由不包含:id段,因此params[:id]nil
但如果您要显示当前用户的“配置文件”,则应从会话中获取该用户:

class StaticPagesController < ApplicationController
  # ...
  def diary
    @user = current_user
    @micropost = @user.microposts.build 
  end
end

您还应该事先执行检查以确保用户已登录。

相关问题