如何将用户 post 的 ID 设置为他们的 post

How to set a users post id to their post

我在将用户状态 post 关联到他们时遇到问题,我也无法在他们的帐户上显示它。

这是我的 users_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 
    @posts = @user.posts
  end 

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

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

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

  def show
    @user = Post.first.update_attributes(user_id: 1)
    @post = Post.first.update_attributes(@signed_in_user)
  end

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

  def edit
  end

  def create
    @post.user = current.user
    @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 = @signed_in_user
    @post = Post.new(post_params)

    if @post.save 
        redirect_to @post 
    else
        render 'new'
    end
end
def show
    @post = Post.find(params[:id])
    @signed_in_user = session[:user_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

这是我希望 signed_in_users 最近 post 返回到的 profile.html.erb 页面

<%- @posts.each do |post| %>
            <div class="post_wrapper">
                <h2 class="title"><%= link_to post.body, post %></h2>
                <p class="date"><%= post.created_at.strftime("%B, %d, %Y") %> </p>
            </div>
<% 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

只是为了更清楚地说明问题并且非常具体,我希望它完全像 Facebook 提供的状态 post,你写它,它 post,它显示在你的个人资料上。就是这样,我在写作、发布和保存方面没有问题。它只是不显示在创建它的用户的个人资料页面上。

您的创建操作应如下所示

def create         
    @post = Post.new(post_params)
    @post.user_id = @signed_in_user.id

    if @post.save 
        redirect_to @post 
    else
        render 'new'
    end
end

试试这个

def create 
    @post = @signed_in_user.posts.build(post_params)
    if @post.save 
        redirect_to @post 
    else
        render 'new'
    end
end
  1. 运行 在您的终端中执行此命令:

    rails generate migration AddUserRefToPosts user:references
    
  2. 在您的用户模型中:

    class User < ActiveRecord::Base
      has_many :posts
      ...
    end
    

3.in post 控制器

def create 

   @post = Post.new(post_params)
   @post.user_id = @signed_in_user.id

   if @post.save 
       redirect_to @post 
   else
       render 'new'
   end
end