1

I'm creating app on Rails with categories, users(authentication with Devise), posts. If user will write post I want to show his name above his post. When another user watching post, he will know who create this post. How can I do this? models:

class User < 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

class Post < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
end

class Category < ActiveRecord::Base
    has_many :posts
end

controllers: postcontroller

def show
        @post = Post.find(params[:id])
        @category = Category.find(params[:category_id])
    end

views/posts/show

<div class="container">
    <div class="post">
        <div class="row">
            <div class="col-md-6">
                <div class="price pull-right">
                <%= @post.price %>
                </div>
                <h3><%= @post.name %></h3>
                    <div class="content">
                        <%= @post.content %>
                    </div>
                <div>
                    <strong>Когда:</strong>
                    <%= @post.date %>
                </div>
            </div>
            <div class="col-md-6">
                <%= post.user.email%>
            </div>
        </div>
    </div>
</div>

<%= post.user.email%>

But it doesn't work.

4

1 回答 1

2

在您的视图结束时,您有post.user.email. 您需要在@user那里使用实例变量。

          <div class="col-md-6">
              <%= @post.user.email%>
          </div>
      </div>
  </div>
</div>

<%= @post.user.email%>
于 2013-11-10T16:42:10.890 回答