0
我有Devise設置,可以創建帖子,但我想添加帖子創建者的用戶名。 我錯過了什麼嗎?Rails 4,設計助理用戶發帖
posts控制器:
def create
@post = current_member.posts.new(params[:post])
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
交模型:
class Post < ActiveRecord::Base
belongs_to :member
end
構件模型:(我使用構件作爲用戶)
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
end
在show.html我不不知道要在屁股上放什麼來顯示用戶名。我得到的錯誤,或者它只會顯示所有帖子當前的用戶電子郵件,直到我註銷,然後當然沒有錯誤。
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<p>
<strong>Author:</strong>
<%= @post.author %>
</p>
<p>
<strong>Main:</strong>
<%= @post.main %>
</p>
<%= current_member.email %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
這樣的運氣呢? – zeantsoi