Loi Huynh
Loi Huynh

Reputation: 448

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

May be a simple question but I'm relatively new to Rails.

I have a post with multiple images that displays all four on the show page:

enter image description here

This is the code:

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

That's the show page. My question is if I only want the first image to display on my index page, how would I alter my code to do so? I've tried this but with no luck:

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

I'm not sure if you need anything else to help me, so just ask if you need my controller or whatever. Thank you!


UPDATE

When I do this:

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

I get this error: enter image description here

Trying this:

.post_image
    = image_tag @post_attachments.first.image_url

Gives me this:

enter image description here

Post Controller

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

Upvotes: 0

Views: 370

Answers (2)

Krule
Krule

Reputation: 6476

Just access first element of collection. Like this:

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

Looking at your index view, remove - @posts.each do |post| and .try(: image_url) should be .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

Using try in case your collection is empty.

Upvotes: 2

Graham S.
Graham S.

Reputation: 1530

Well it appears that you're calling the posts#index action and not the posts#show action. In your index action your do not have a @post_attachments variable, only a @posts variable.

I believe you may have a routing issue. Check and make sure that you are calling the correctly action in your PostsController.

Upvotes: 0

Related Questions