我在RailsTutorial
Michael Hartl
chapter 10
上Micropost
并且做了一些额外的事情来为每个微博添加回形针图像。当我向Microphoto
添加照片时,我将其称为class AddPhotoclipToMicrophotos < ActiveRecord::Migration
def self.up
add_column :microphotos, :photoclip_file_name, :string
add_column :microphotos, :photoclip_content_type, :string
add_column :microphotos, :photoclip_file_size, :integer
add_column :microphotos, :photoclip_updated_at, :datetime
end
def self.down
remove_column :microphotos, :photoclip_updated_at
remove_column :microphotos, :photoclip_file_name
remove_column :microphotos, :photoclip_content_type
remove_column :microphotos, :photoclip_file_size
end
end
现在我做了以下事情:
正确迁移数据库
class User < ActiveRecord::Base
require "paperclip"
attr_accessible :name, :email, :password, :password_confirmation, :avatar,
:avatar_file_name, :avatar_content_type, :avatar_file_size, :avatar_updated_at
has_secure_password
has_many :microphotos, dependent: :destroy
has_attached_file :avatar, :styles => { :large => "120x120>", :medium => "48x48>", :thumb => "26x26>" }
.
.
.
def feed
# This is preliminary. See "Following users" for the full implementation.
Microphoto.where("user_id = ?", id)
end
end
更改微照片模型和控制器
这是我的档案:
用户模型
class Microphoto < ActiveRecord::Base
attr_accessible :content, :votes_down, :votes_up, :photoclip,
:photoclip_file_name, :photoclip_content_type, :photoclip_file_size, :photoclip_updated_at
belongs_to :user
has_attached_file :photoclip, :styles => { :large => "500x400>", :medium => "100x80>" }
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
default_scope order: 'microphotos.created_at DESC'
end
Microphoto模型
class MicrophotosController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def index
end
def create
@microphoto = current_user.microphotos.build(params[:microphoto])
if @microphoto.save
flash[:success] = "Your photo has been uploaded successfully!"
redirect_to root_url
else
render 'static_pages/home'
end
end
def destroy
end
end
Microphotos Controller
/views/static_pages/home.html.erb
/views/shared/_feed.html.erb
&amp; /views/shared/_feed_item.html.erb
&amp; /views/users/show.html.erb
个文件。_microphoto.html.erb
&amp; <% if signed_in? %>
<div class="span8">
<h3>Uploaded Photos</h3>
<%= render 'shared/feed' %>
</div>
<% else %>
<div>Some static page datat</div>
<% end %>
个文件。 views / static_pages / home.html.erb(Homepage)
<% if @feed_items.any? %>
<ol class="microphotos">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
views / shared / _feed.html.erb
<li id="<%= feed_item.id %>">
<%= image_tag feed_item.photoclip.url(:large) %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
</span>
</li>
views / shared / _feed_item.html.erb
<div class="row">
<div class="span8">
<% if @user.microphotos.any? %>
<h3>Microphotos (<%= @user.microphotos.count %>)</h3>
<ol class="microphotos">
<%= render @microphotos %>
</ol>
<%= will_paginate @microphotos %>
<% end %>
</div>
</div>
views / users / show.html.erb
<li>
<%= image_tag @user.microphotos.photoclip.url(:medium) %>
<span class="content"><%= microphoto.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(microphoto.created_at) %> ago.
</span>
</li>
views / microphotos / _microphoto.html.erb
http://localhost:3000/users/1
所以访问地址:
def feed
Microphoto
end
,它给了我以下错误:
未定义的方法`photoclip'for - ActiveRecord :: Relation:0xb50829f0
但每张microhoto都有数据库中的照片。
为了在主页上显示所有用户的微照片我使用了这个:
User model
在我的{{1}}。
答案 0 :(得分:0)
这是因为@user.microphotos
设置为microphotos
而不是单microphoto
条记录。你应该做:
<%= render @user.microphotos %>
</ol>
<%= will_paginate @user.microphotos %>
在views/users/show.html.erb
和
<%= image_tag microphoto.photoclip.url(:medium) %>
views/microphotos/_microphoto.html.erb
中的。
要在home
页面中包含所有微照片,您应该这样做:
def feed
Microphoto.all
end
在User
模型中。
答案 1 :(得分:0)
@user.microphotos
是一种关系,意味着与用户相关的一系列照片。你想要做的是迭代@user.microphotos
并显示每张照片,就像这样
<% image_tag @user.microphotos.each do |photoclip| %>
<li>
<%= image_tag photoclip.url(:medium) %>
<span class="content"><%= photoclip.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(photoclip.created_at) %> ago.
</span>
</li>
<% end %>