1

I'm creating this rails app, In the app I have the functionality to make an account, then post a status. I have made it so I can display the status on the screen, but how do i display the name of the person who made the post? I am using devise and have setup :username

My View

<% if user_signed_in? %>
  <h1 id="welcome" class="nuvo">Welcome <%= current_user.username %>!</h1>
<% else %>
  <h1 id="welcome" class="nuvo">Log-In to make some posts!</h1>
<% end%>

<div class="follow-row">
  <div class="titan-users nuvo"><h2>TITAN Users</h2></div>
</div>

<div class="statuses">
  <% if user_signed_in? %><div class="status-form"><%= render 'form' %></div><% end %>
  <% @posts.each do |post| %>
    <div class="post">
      <div class="tstamp"><strong>Posted <%= time_ago_in_words(post.created_at) %> ago by <%= current_user.username %></strong></div>
      <div class="status"><%= post.status %></div>
    </div>
  <% end %>
</div>

My Post Controller

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all(:order => "created_at DESC")
    @post = Post.new

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    redirect_to posts_path
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end

My user model

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username

  has_many :post
  # attr_accessible :title, :body
end

My Post model

class Post < ActiveRecord::Base
  attr_accessible :status, :author

  belongs_to :user

  validates :status, :presence => true
end

So, does anyone have any ideas how in the view instead displaying, 'current_user.username' can I display the name of the person who posted it?

So, for CodeIt this is the error I get

undefined method `username' for nil:NilClass
Extracted source (around line #17):

14:   <% if user_signed_in? %><div class="status-form"><%= render 'form' %></div><% end %>
15:   <% @posts.each do |post| %>
16:     <div class="post">
17:       <div class="tstamp"><strong>Posted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.username %></strong></div>
18:       <div class="status"><%= post.status %></div>
19:     </div>
20:   <% end %>
20:   <% end %>`
4

2 回答 2

1

你有postbelongs_to user。所以你可以使用:

 post.user.username  #In your @posts loop
于 2013-03-17T14:50:58.650 回答
0

在控制器中的 create 方法中,我没有看到您将 user_id 传递给它,因此可能是用户 dint 设置到 post 模型中。

也许您可以在表单中放置一个 hidden_​​field_tag,并将您的 user_id 作为参数传递。然后,在您的控制器中,执行类似 @post.user_id = params[:user_id] 的操作

然后,在您看来,您可以访问 user_id 并从 user_id 中找到用户名

于 2013-03-17T15:20:38.663 回答