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  ...

controller/post -- 部分クラス 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) は、それらのコレクション全体ではなく、単一の投稿をレンダリングする必要があります。2 番目の問題は、パーシャルに何も渡していないことです。

コレクションをレンダリングするための便利な省略形があります。コレクションをレンダリングするだけで、自動的に同じ名前のパーシャルが検索され、コレクション内のモデルごとにレンダリングされます。

参照: 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 に答える