Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:58:41+00:00 2026-06-08T01:58:41+00:00

I am trying a an simple association which should work. I followed the tutorial

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:58:43+00:00Added an answer on June 8, 2026 at 1:58 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying this simple tutorial from oracle : http://www.oracle.com/technetwork/java/socket-140484.html (the Example 1 ).
I'm trying to create a simple merch association between two Products. I've followed a
I am learning and trying simple example using node.js and mongoskin. here is my
I'm trying to make simple many-to-one association, using NHibernate.. I have class Recruit with
I'm trying to setup a simple has_many :through => association. I have 3 models.
I'm trying to set up a simple association in NHibernate (this is the first
I am trying to work through simple examples to see all the changes in
I am trying I have a simple one-to-many association. I am trying to update
I'm trying to set up a polymorphic association for photo uploads which are processed
After trying this simple console input with 5, the result is shown as 53

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.