Ruby on Rails 协会 [英] Association Ruby on Rails
问题描述
我正在尝试一个应该可以工作的简单关联.我在 http://ruby.railstutorial.org/chapters/ 和第 10 章
这里是我写的
模型/客户
class Customer
模型/帖子
class Post
控制器/客户
class CustomersController <应用控制器before_filter :signed_in_customer, 仅: [:edit, :update]before_filter :correct_customer, only: [:edit, :update]定义索引@customers = Customer.all结尾高清秀@customer = Customer.find(params[:id])@posts = @customer.posts结尾 ...
controller/post -- 应该无关紧要,因为我正在做部分类 PostsController <应用控制器
# GET/posts# 获取/posts.json定义索引@posts = Post.allresponse_to do |格式|format.html # index.html.erbformat.json { 渲染 json: @posts }结尾结尾# 获取/posts/1# 获取/posts/1.json高清秀@post = Post.find(params[:id])response_to do |格式|format.html # show.html.erbformat.json { 渲染 json: @post }结尾结尾# 获取/帖子/新# 获取/posts/new.json定义新@post = Post.newresponse_to do |格式|format.html # new.html.erbformat.json { 渲染 json: @post }结尾结尾# 获取/posts/1/edit定义编辑@post = Post.find(params[:id])结尾# 发布/发布# POST/posts.json定义创建@post = Post.new(params[:post])response_to do |格式|如果@post.saveformat.html { redirect_to @post,注意:'帖子已成功创建.'}format.json { 渲染 json: @post, 状态: :created, location: @post }别的format.html { 渲染动作:新"}format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }结尾结尾结尾# 放置/posts/1# PUT/posts/1.json定义更新@post = Post.find(params[:id])response_to do |格式|如果@post.update_attributes(params[:post])format.html { redirect_to @post,注意:'帖子已成功更新.'}format.json { 头:no_content }别的format.html { 渲染动作:编辑"}format.json { 渲染 json: @post.errors, 状态: :unprocessable_entity }结尾结尾结尾# 删除/posts/1# 删除/posts/1.json销毁@post = Post.find(params[:id])@post.destroyresponse_to do |格式|format.html { redirect_to posts_url }format.json { 头:no_content }结尾结尾结尾
view/customer/show.html.erb
<%= 渲染 'posts/post'%>
view/post/_post.html.erb
<%= @posts.each 做 |post|%><%= post.title %><%结束%>
这是输出的样子
- []
为什么?
谢谢
在我看来,您似乎搞错了几件事.首先,部分 (view/post/_post.html.erb) 应该呈现单个帖子,而不是它们的整个集合.第二个问题是你没有将任何东西传递给部分.
渲染集合有一个方便的简写方式,如果您只是渲染集合,它会自动查找具有相同名称的部分并为集合中的每个模型渲染它.
参见:http://guides.rubyonrails.org/layouts_and_rendering.html#using-部分
所以我认为这应该可以满足您的需求(我添加了
标签以使其成为无序列表):
view/customer/show.html.erb
<ul><%= 渲染@posts %>
view/post/_post.html.erb
<%= post.title %>
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
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>
这篇关于Ruby on Rails 协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!