2013-12-10 57 views
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 %> 
+0

这样的运气呢? – zeantsoi

回答

1

我认为你不希望登录成员/用户的电子邮件与帖子,而是作者的电子邮件。尝试:@post.member.email