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

时间:2015-02-18 07:03:48

标签: ruby-on-rails status

我在将用户状态信息与他们关联时遇到问题,我也无法将其显示在他们的帐户中。

这是在我的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最新帖子返回的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提供的状态信息完全一样,你写的,它发布,它显示在你的个人资料上。就是这样,我在写作,发布和保存方面没有任何问题。它只是没有显示在创建它的用户的配置文件页面上。

3 个答案:

答案 0 :(得分:2)

  1. 在终端中运行此命令:

    rails generate migration AddUserRefToPosts user:references
    
  2. 您的用户模型中的
  3. class User < ActiveRecord::Base
      has_many :posts
      ...
    end
    
  4. 3.in post controller

    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
    

答案 1 :(得分:0)

您的创建操作应如下所示

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

答案 2 :(得分:0)

试试这个

def create 
    @post = @signed_in_user.posts.build(post_params)
    if @post.save 
        redirect_to @post 
    else
        render 'new'
    end
end