Association Ruby on Rails

前提是你 提交于 2019-12-11 18:26:44

问题


I am trying a an simple association which should work. I followed the tutorial at http://ruby.railstutorial.org/chapters/ and at chapter 10

Here what I have wrote down

model/customer

class Customer < ActiveRecord::Base

    has_many :posts, dependent: :destroy


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

model/post

class Post < ActiveRecord::Base

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

controller/customers

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 -- should be irrelevant since i am doing a partial class 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

view/customer/show.html.erb

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

view/post/_post.html.erb

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

Here what the output look like

  • []

Why?

Thanks


回答1:


It looks to me like you've got a couple things wrong. First, the partial (view/post/_post.html.erb) should be rendering a single post, not the whole collection of them. Second problem is that you're not passing anything to the partial.

There's a handy shorthand for rendering collections, where if you just render the collection, it will automatically look for a partial with the same name and render it for each model in the collection.

See: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

So I think this should do what you want (I added <ul></ul> tags to make it an unordered list):

view/customer/show.html.erb

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

view/post/_post.html.erb

<li>
  <%= post.title %>
</li>



回答2:


It looks like customer doesn't have a posts association, but only an events association. I'd start there



来源:https://stackoverflow.com/questions/11572293/association-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!