Ruby on Rails,带有照片的Microposts

时间:2013-06-25 08:37:21

标签: ruby-on-rails paperclip

我在RailsTutorial Michael Hartl chapter 10Micropost并且做了一些额外的事情来为每个微博添加回形针图像。当我向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 现在我做了以下事情:

  1. 正确迁移数据库

    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
    
  2. 更改微照片模型和控制器

  3. 写一些观点
  4. 这是我的档案:

    用户模型

    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

    我想在我的观点中有两件事:

    • 仅针对signed_in用户
    • 显示其主页上所有用户的所有微照片列表
    • 仅针对signed_in用户,在他/她自己的页面上显示当前用户的所有微照片列表 对于第一个,我有/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}}。

2 个答案:

答案 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 %>