我无法一遍又一遍地找到帖子' id' =我正在尝试将由登录用户创建的帖子呈现给他们的个人资料页面。
这是我的整个User_controller,
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
end
def profile
@user = User.find(session[:user_id]) unless session[:user_id] == ""
redirect_to login_path, notice: "You're not logged in" unless @user
@post = Post.find(params[:id])
end
def destroy
@user = User.find(session[:user_id])
end
def welcome
@user = User.find(session[:user_id])
end
def show
@user = User.find(params[:user_id])
@posts = @user.posts
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to profile_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 @user, 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(:name, :password, :password_confirmation, :email, :age, :profile_picture, :post)
end
end
我的Posts_controller中还有以下内容,
class PostsController < ApplicationController
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:body))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:body)
end
end
以下是我的路线文件
Rails.application.routes.draw do
root 'welcome#welcome'
get 'login' => 'sessions#login', :as => :login
get 'profile' => 'users#profile', :as => :profile
post 'logging/user' => 'sessions#create'
get 'logout' => 'sessions#destroy', :as => :logout
get 'about' => 'about'
resources :users
resources :posts
get 'index' => 'posts#index'
get 'register' => 'users#new', :as => :register
这是我的帖子模型,
class Post < ActiveRecord::Base
belongs_to :user
validates :body, presence: true
end
这是我的用户模型
class User < ActiveRecord::Base
has_secure_password
validates :email, :name, 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 => "300x200>" },
: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
如果你能给我一些建议,那就非常有帮助!我好几天都被这个小问题困住了!
答案 0 :(得分:1)
实际上,您不会在个人资料页面中呈现用户帖子:您有
@post = Post.find(params[:id])
在您的控制器的profile
操作中。
你显然没有在你的路线上有一个post id param。
只需用
替换这一行@posts = @user.posts
就像你在show
行动中所做的那样。
此外,如果您在profile
中呈现完全相同的内容,我不确定您是否真的需要此自定义show
操作。