简体   繁体   中英

Using partials in Rails 6 to divide files

I have a photo_upload.html.erb file where I list all of my files.

<div id="photos"><%= render partial: "rooms/photos", locals: { photos: @room.images } %></div>

Notice that I passed in photos in the locals.

I then created a new file _photos.html.erb and place the ff code:

<% if @photos.count > 0 %>
<br/><br/>

<div class="row">
<% @photos.each do |photo| %>
   <div class="col-md-4">
     <%= render partial: 'rooms/photo', locals: { photo: photo } %>
  </div>
<% end %>
</div>
<% end %>

And then added another partial for each individual photo by create a new file _photo.html.erb where I place the ff codes:

 <%= image_tag(photo,  class: "test_image") %>
  <%= link_to "Remove", delete_image_attachment_rooms_path(photo), class: 'delete', remote: true, method: :delete, data: { confirm: "Are you sure you want to delete this?" } %>

Note that all files sits in the same folder rooms . However, this can't read the photo or photos even though I already passed them in to the locals.

Here's my controller for your reference:

class RoomsController < ApplicationController
  before_action :set_room, except: [:index, :new, :create, :delete_image_attachment]
  before_action :authenticate_user!, except: [:show]

  def index
    @rooms = currrent_user.rooms
  end

  def new
    @room = current_user.rooms.create
  end

  def create
    @room = current_user.rooms.create(room_params)

    if @room.save
      redirect_to listing_room_path(@room), notice: "Saved..."
    else 
      flash[:alert] = "Something went wrong!"
      render :new
    end 
  end

  def show
  end 

  def listing
  end

  def pricing
  end

  def description
  end

  def photo_upload
  end

  def amenities
  end

  def location
  end

  def update
    if @room.update(room_params)
      @room.images.attach(params[:room][:room_images])
      
      flash[:notice] = "Saved..."
    else 
      flash[:alert] = "Something went wrong..."
    end 
    redirect_back(fallback_location: request.referrer)
  end

  def delete_image_attachment
    @image = ActiveStorage::Attachment.find_by(params[:id])
    @image&.purge
    # flash[:notice] = "Image deleted..."
    # redirect_back(fallback_location: request.referrer)
    respond_to do |format|
      format.js 
    end
  end

  private


  def set_room
    @room = Room.find(params[:id])
  end 

  def room_params
    params.require(:room).permit(:home_type, :room_type, :accomodate, :bed_room, :bath_room, :listing_name, :summary, :address, :is_tv, :is_kitchen, :is_air, :is_heating, :is_internet, :price, :active, images: [])
  end 
end

I am wondering what am I doing wrong here in order to divide the partials and still can make it work. Am I doing something wrong with the locals and partial naming?

Please help!

When you pass a local variable into a partial, it does not have an @ prefix. @photos is not how you use a local delivered by locals: { photos: ... } , you need photos .

Exactly how you use photo in your _photos partial, and not @photo .

Note also that you're using a dated syntax. You can replace these...

render partial: "rooms/photos", locals: { photos: @room.images }
render partial: 'rooms/photo', locals: { photo: photo }

with these:

render 'rooms/photos', photos: @room.images
render 'rooms/photo', photo: photo

The results are identical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM