[英]Ruby on Rails, Microposts with Photo
我在chapter 10
的Michael Hartl
上的RailsTutorial
上,做了一些额外的工作,为每个RailsTutorial
添加了回形针图像。 当我向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
Microphotos控制器
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
文件。 views / 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 %>
views / shared / _feed.html.erb
<% if @feed_items.any? %>
<ol class="microphotos">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
views / shared / _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>
views / users / 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>
views / 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
,它给我以下错误:
-ActiveRecord :: Relation:0xb50829f0的未定义方法`photoclip'
但是每张显微照片在数据库中都有一个回形针。
为了显示主页上所有用户的显微照片,我使用了以下方法:
def feed
Microphoto
end
在我的User model
。
这是因为@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
模型中。
@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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.