gpt4 book ai didi

ruby-on-rails - 用户无法访问我的 ruby​​ 应用程序中的其他用户

转载 作者:行者123 更新时间:2023-12-03 08:35:44 25 4
gpt4 key购买 nike

我环顾四周,发现只有一个问题和我问的问题一样,而且其中的信息对我的问题没有帮助。我遇到的问题是我的用户,我有几个用户。您可以导航到显示每个用户的页面,每个用户都是可点击的。但是,每当我登录到一个用户时,说 Profile(1) 并且我想访问 Profile(2) 页面,它只会加载 Profile(1) 的凭据。年龄、生物和图片。我的问题是什么?这可能是一个简单的修复,如果是这样,那么提前感谢您的帮助!

这是列出所有用户的页面index.html.erb,

<div class="all_users">
<div class="container">
<p id="notice"><%= notice %></p>

<h1>Listing Users</h1>

<table>
<thead>
<tr>

<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @users.each do |user| %>
<ul class="media-list">
<li class="media">
<div class="media-left">
<%= link_to image_tag user.profile_picture.url(:thumb) %>
</a>
</div>
<div class="media-body">
<h4 class="media-heading">Name: <%= link_to user.first_name, dashboard_path %></h4>
<p>Age: <%= user.age %></p>
<p><%= user.bio %></p>
</div>
</li>
</ul>
<tr>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', @user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br/>

<%= link_to 'New User', new_user_path %>
<%= link_to 'Login', login_path %>
</div>
</div>

这里是用户show.html.erb的个人资料页面,

<div class="show">

<div class="col-md-3">
<div class="thumbnail">


<% else %>
<%= image_tag @user.profile_picture.url(:medium) %>


<div class="caption">
<h2><%= @user.name %></h2>
<p>Message This User <i class="fa fa-envelope-o fa-lg"> </i></p>
<p>Report This User <i class="fa fa-exclamation fa-lg"> </i></p>
<p>Block this User <i class="fa fa-user-times fa-lg"> </i></p> <p><a href="#" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">Add As Friend</a> <a href="#" class="btn btn-default" role="button">Follow</a></p>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Bio for <%= @user.first_name %></div>
<p class="bio">
<%= @user.bio %>
</p>
</div>
</div>

<div class="col-md-4">
<div class="panel panel-default">
<%= form_for :post, url: posts_path do |f| %>
<div class="panel-heading">
<i class="fa fa-pencil-square"></i> <%= f.label :Status %> |<br>
</div>
<div class="panel-body">
<%= f.text_area :body, placeholder: "What's new?" %>
</div>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
<%- @posts.each do |post| %>
<div class="panel panel-default">

<div class="panel-heading-gray"> <%= image_tag @user.profile_picture.url(:thumb) %> <h5 class="user-name"><%= @user.name %></h5> <h6 class="time-posted"><%= post.created_at.strftime("%B, %d, %Y") %></h6></div>
<div class="panel-body"><%= link_to post.body, post %></div>
<div class="panel-footer">

<%= render post.comments %>
<p class="Like-option">Like ·</p>
<p class="comment-form">Comment</p>
<p class="view-option">· View</p>
<p class="comment-profile-picture">
<%= image_tag @user.profile_picture.url(:thumb) %>
</p>
<%= @post.comments.count %>
<div id="comments-form">
<%= render "comments/form", :post => post %>

</div>
</div>
</div>
<% end %>
</div>

这是我的 users_controller.rb,

class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
after_action :signed_in_after_register, only: :create

def index
@users = User.all
@user = User.find(session[:user_id])
end

def dashboard
@user = User.find(session[:user_id]) unless session[:user_id] == ""
redirect_to login_path, notice: "You're not logged in" unless @user
@posts = @user.posts.order("created_at DESC").limit(3)
@comment = Comment.new
@post = Post.new
end

def newsfeed
@user = User.find(session[:user_id]) unless session[:user_id] == nil
redirect_to login_path, notice: "You're not logged in" unless @user
@posts = @user.posts.order("created_at DESC").limit(3)
end

def nav
@user = User.find(session[:user_id])
end
def posts
@user = User.find(session[:user_id])
@posts = @user.posts
end

def destroy
@user = User.find(session[:user_id]) unless session[:user_id] == ""
redirect_to login_path, notice: "You're not logged in" unless @user
end

def welcome
@user = User.find(session[:user_id]) unless session[:user_id] == ""
redirect_to login_path, notice: "You're not logged in" unless @user
end

def show
@user = User.find(params[:user_id]) unless session[:user_id] == ""
redirect_to login_path, notice: "You're not logged in" unless @user
@posts = @user.posts.order("created_at DESC").limit(3)
@comment = Comment.new
@post = Post.new
end

def new
@post = Post.new(params[:post_id])
@user = User.new
end

def edit

end

def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.html { redirect_to dashboard_path, notice: 'User was successfully created!' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

def update

respond_to do |format|
if @user.update(user_params)
format.html { redirect_to dashboard_path, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end

private
def set_user
@user = User.find(params[:id])
end
def signed_in_after_register
session[:user_id] = @user.id
end

def user_params
params.require(:user).permit(:first_name, :last_name, :bio, :password, :password_confirmation, :email, :age, :profile_picture, :post, :body)
end
end

这是我的用户模型 user.rb,

class User < ActiveRecord::Base
has_secure_password
validates :first_name, :last_name, :email, presence: true, uniqueness: true
validates_inclusion_of :age, in: 10..100
validates :password, presence: true
has_many :posts
has_attached_file :profile_picture, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "app/assets/images/missing.png",
:path => ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename"
validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/

我尝试输入 User.find(params[:user_id]) 并收到以下错误

ActiveRecord::RecordNotFound in UsersController#show 
Couldn't find User with 'id'=

编辑:这是我的路线文件,

Rails.application.routes.draw do

root 'welcome#welcome'

get 'login' => 'sessions#login', :as => :login

get 'dashboard' => 'users#dashboard', :as => :dashboard

post 'logging/user' => 'sessions#create'

get 'logout' => 'sessions#destroy', :as => :logout

get 'about' => 'about'

get 'newsfeed' => 'users#newsfeed'

resources :users, except: :show
get 'profile/:id' => 'users#show', as: :profile

resources :posts do
resources :comments
end

get 'index' => 'posts#index'

get 'register' => 'users#new', :as => :register

end

最佳答案

您要单击哪个链接来查看用户的个人资料?

<h4 class="media-heading">Name: <%= link_to user.first_name, dashboard_path %></h4>

或者:

<td><%= link_to 'Show', user %></td>

如果它是第一个,为什么要链接到 dashboard_path?看起来应该链接到 user_path(user) 对吧?

也可能有助于向我们展示您的路线文件。

关于ruby-on-rails - 用户无法访问我的 ruby​​ 应用程序中的其他用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32288921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com