Carrierwave and Rails 4 多图上传获取第一张图片只显示

Carrierwave and Rails 4 Multiple Image uploading getting the first image only to display

可能是一个简单的问题,但我对 Rails 还比较陌生。

我有一个 post 有多个图像,显示页面上显示所有四个:

这是代码:

- @post_attachments.each do |p|
                = image_tag p.image_url

这是展示页面。我的问题是,如果我只希望第一张图片显示在我的索引页上,我该如何更改我的代码才能做到这一点?我已经试过了,但没有成功:

- @post_attachments.each do |p|
                        = image_tag p.image_url.first

我不确定您是否需要其他任何帮助,所以请问您是否需要我的控制器或其他任何东西。谢谢!


更新

当我这样做时:

- @post_attachments.each do |p|
                        = image_tag @post_attachments.first.image_url

我收到此错误:

试试这个:

.post_image
    = image_tag @post_attachments.first.image_url

给我这个:

Post 控制器

class PostsController < ApplicationController
    before_action :find_posts, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
    before_action :authenticate_user!, except: [:index, :show, :home]

    def home
    end

    def index
        if params[:category].blank?
            @posts = Post.all.order("created_at DESC")
        else
            @category_id = Category.find_by(name: params[:category]).id  
            @posts = Post.where(category_id: @category_id).order("created_at DESC")

        end
    end

    def show
        @inquiries = Inquiry.where(post_id: @post).order("created_at DESC")
        @random_post = Post.where.not(id: @post).order("RANDOM()").first
        @post_attachments = @post.post_attachments.all
    end

    def new
        @post = current_user.posts.build
        @post_attachment = @post.post_attachments.build
    end

    def create
        @post = current_user.posts.build(post_params)
        respond_to do |format|
            if @post.save 
                params[:post_attachments]['image'].each do |a|
                    @post_attachment = @post.post_attachments.create!(:image => a)
                end
                format.html { redirect_to @post, notice: 'Post was successfully created.' }
            else 
                format.html { render action: 'new' }
            end
        end
    end

    def edit
    end

    def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

    def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

    def upvote
        @post.upvote_by current_user
        redirect_to @post
    end

    def downvote
        @post.downvote_by current_user
        redirect_to @post
    end

    private 
    def find_posts 
        @post = Post.find(params[:id])
    end

    def post_params
        params.require(:post).permit(:title, :price, :description, :location, :category_name, :contact_number, :image)
    end
end

index.html.haml

#posts
    .row
        - @posts.each do |post|
            .col-xs-12.col-sm-6.col-md-4.col-lg-3
                .post
                    .post_content
                        .title
                            %h2
                                = link_to truncate((post.title), length: 25), post
                        %p
                            $
                            = post.price
                        .post_image
                            - @posts.each do |post| 
                                = post.post_attachments.try(:first).try(: image_url)

                        .data.clearfix
                            %p.username
                                Listing by
                                = post.user.name
                            %p.buttons
                                %span
                                    %i.fa.fa-comments-o
                                    = post.inquiries.count
                                    |
                                    %i.fa.fa-thumbs-o-up
                                    = post.get_likes.size
                                    |
                                    %i.fa.fa-thumbs-o-down
                                    = post.get_dislikes.size

只需访问 collection 的第一个元素。像这样:

= image_tag(@post_attachments.first.try(:image_url))

查看您的索引视图,删除 - @posts.each do |post| 并且 .try(: image_url) 应该是 .try(:image_url)

#posts
  .row
    - @posts.each do |post|
      .col-xs-12.col-sm-6.col-md-4.col-lg-3
        .post
          .post_content
            .title
              %h2
                = link_to truncate((post.title), length: 25), post
            %p
              $
              = post.price
            .post_image
                = post.post_attachments.try(:first).try(: image_url)

            .data.clearfix
              %p.username
                Listing by
                = post.user.name
              %p.buttons
                %span
                  %i.fa.fa-comments-o
                  = post.inquiries.count
                  |
                  %i.fa.fa-thumbs-o-up
                  = post.get_likes.size
                  |
                  %i.fa.fa-thumbs-o-down
                  = post.get_dislikes.size

使用 try 以防您的 collection 为空。

看来您调用的是 posts#index 操作,而不是 posts#show 操作。在您的 index 操作中,您 没有 有一个 @post_attachments 变量,只有一个 @posts 变量。

我相信您可能遇到了路由问题。检查并确保您在 PostsController.

中调用了正确的操作