Carrierwave和Rails 4多张图片上传只显示第一张图片

时间:2015-08-24 19:51:38

标签: ruby-on-rails ruby ruby-on-rails-4 carrierwave

可能是一个简单的问题,但我对Rails相对较新。

我有一个包含多个图片的帖子,在显示页面上显示所有四个图片:

enter image description here

这是代码:

- @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

我收到此错误:enter image description here

试试这个:

.post_image
    = image_tag @post_attachments.first.image_url

给我这个:

enter image description here

后置控制器

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

1 个答案:

答案 0 :(得分:2)

只需访问集合的第一个元素即可。像这样:

= 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