我一直在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
显微照片控制器
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
我想在我的观点中有两件事:
- 仅在登录用户的主页上显示所有用户的所有微照片列表
- 在他/她自己的页面上显示当前用户的所有微照片列表,仅适用于已登录用户
对于第一个我有/views/static_pages/home.html.erb
&/views/shared/_feed.html.erb
&/views/shared/_feed_item.html.erb
文件。
对于第二个,我有/views/users/show.html.erb
&_microphoto.html.erb
文件。
视图/static_pages/home.html.erb(主页)
<% if signed_in? %>
<div class="span8">
<h3>Uploaded Photos</h3>
<%= render 'shared/feed' %>
</div>
<% else %>
<div>Some static page datat</div>
<% end %>
意见/共享/_feed.html.erb
<% if @feed_items.any? %>
<ol class="microphotos">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
意见/共享/_feed_item.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>
我得到了第一件事,但第二件事正在等待中......
意见/用户/show.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>
意见/microphotos/_microphoto.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>
所以通过访问地址:
http://localhost:3000/users/1
,它给了我以下错误:
未定义的方法`photoclip' - ActiveRecord::Relation:0xb50829f0
但是每张缩微照片在数据库中都有一个照片夹。
更新
为了在主页上显示所有用户的微照片,我使用了这个:
def feed
Microphoto
end
在我的User model
.