我老老实实地到处寻找修复这个错误,人们有类似的问题,但我找不到任何帮助我解决我的问题。我正在尝试以其他用户身份登录时查看其他用户个人资料页面。没有设备,为此。我一直在遇到与此完全相同的错误。
这是我的users_controller.rb,
int months = 0;
for (; months< 12; )
{
td = td.AddMonths(1);
if (td > next)
{
td = td.AddMonths(-1);
break;
}
months++;
}
这是我的用户模型user.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(params[: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
@user = User.new
@post = Post.new(params[:post_id])
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 :profile, 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 :profile, 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
这是我的routes.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, length: { minimum: 4 }, allow_nil: 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/
end
我愿意发布您可能需要查看的任何代码,以进一步帮助解决我的问题。感谢任何帮助,我一直试图解决这个问题3个星期。先谢谢你了!
答案 0 :(得分:3)
您的控制器在点击您的操作代码之前正在寻找“id”,通过..
before_action :set_user, only: [:show, :edit, :update, :destroy]
..反过来调用“set_user”方法并尝试...
User.find(params[:id])
但是,您的路由将参数定义为“:user_id”,而不是“:id”...
get 'profile/:user_id' => 'users#show', as: :profile
由于您在操作中编写了自己的.find查询,请尝试从before_action中删除“:show”。或者你可以干一点,只是在任何地方使用“:id”以避免混淆。 :)
答案 1 :(得分:0)
正如我在这里看到的那样,你在Params中提供了一个字段user_id而不是id。你应该在url本身发送id。
ex:localhost:3000 / user / 12
同样在你的控制器动作中使用params [:id]而不是params [:user_id]