0

我正在尝试一个应该有效的简单关联。我遵循了http://ruby.railstutorial.org/chapters/和第 10 章的教程

这里是我写下的

型号/客户

class Customer < ActiveRecord::Base

    has_many :posts, dependent: :destroy


  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

模型/帖子

class Post < ActiveRecord::Base

    belongs_to :customer
    attr_accessible :description, :post_finish_at, :post_how, :post_location

控制器/客户

class CustomersController < ApplicationController

  before_filter :signed_in_customer, only: [:edit, :update]
  before_filter :correct_customer,   only: [:edit, :update]


  def index
    @customers = Customer.all
  end

  def show
    @customer = Customer.find(params[:id])
    @posts = @customer.posts
  end  ...

控制器/帖子——应该无关紧要,因为我正在做一个部分类 PostsController < ApplicationController

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    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
    @post = Post.find(params[:id])
        respond_to do |format|
            format.html # show.html.erb
            format.json { render json: @post }
        end
  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

查看/客户/show.html.erb

<div class="posts">
    <%= render 'posts/post'%>
</div>

查看/发布/_post.html.erb

<li>
    <%= @posts.each do |post| %>
        <%= post.title %>
    <% end %>
</li>

这里的输出是什么样的

  • []

为什么?

谢谢

4

2 回答 2

0

看起来客户没有帖子关联,而只有事件关联。我会从那里开始

于 2012-07-20T03:33:46.253 回答
0

在我看来,你有几件事错了。首先,部分 (view/post/_post.html.erb) 应该呈现单个帖子,而不是它们的整个集合。第二个问题是你没有向部分传递任何东西。

渲染集合有一个方便的简写,如果你只是渲染集合,它会自动寻找具有相同名称的部分,并为集合中的每个模型渲染它。

请参阅: http: //guides.rubyonrails.org/layouts_and_rendering.html#using-partials

所以我认为这应该做你想要的(我添加了<ul></ul>标签以使其成为无序列表):

查看/客户/show.html.erb

<div class="posts">
  <ul>
  <%= render @posts %>
  </ul>
</div>

查看/发布/_post.html.erb

<li>
  <%= post.title %>
</li>
于 2012-07-20T03:45:34.397 回答