我正在创建这个rails应用, 在应用程序中,我具有创建帐户的功能,然后发布状态。 我已经做到了所以我可以在屏幕上显示状态,但是我如何显示发布帖子的人的姓名?我正在使用设计并设置:用户名
我的观点
<% 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>
我的帖子控制器
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
我的用户模型
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
我的帖子模型
class Post < ActiveRecord::Base
attr_accessible :status, :author
belongs_to :user
validates :status, :presence => true
end
那么,有没有人有任何想法如何在视图中显示,'current_user.username'可以显示发布它的人的姓名吗?
因此,对于CodeIt,这是我得到的错误
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 %>`
答案 0 :(得分:1)
您拥有post
belongs_to user
。所以你可以使用:
post.user.username #In your @posts loop
答案 1 :(得分:0)
在控制器的create方法中,我没有看到你将user_id传递给它,所以可能是用户dint设置为post模型。
也许您可以在表单中放入hidden_field_tag,并将您的user_id作为参数传递。 然后,在您的控制器中,执行类似的操作 @ post.user_id = params [:user_id]
然后,在您的视图中,您可以访问user_id并从user_id
中查找用户名