0

I want to display a particular image on my landing page that I have uploaded through the dragonfly gem.

I am getting following error: param is missing or the value is empty: photo

this is what I have on my landing.html.erb view:

<%= image_tag @photo.1.image.thumb('150x185#').url if @photo.1.image_stored? %>

this is what I have on my pages controller:

class PagesController < ApplicationController

    def landing
        @photo = Photo.find(photo_params)
    end


    def photo_params
      params.require(:photo).permit(:image)
    end

end

How do I pass an id of 1 to my @photo object? I know this might be a newb question but I have spent several hours trying different things and nothing has worked. Thanks!

These are my routes:

  root 'pages#landing'

  match '/home',    to: 'pages#home',    via: 'get'
  match '/about',   to: 'pages#about',     via: 'get'
  match '/gallery',   to: 'pages#gallery',   via: 'get'

  match '/events',   to: 'pages#events',   via: 'get'
  match '/menu',   to: 'pages#menu',   via: 'get'
  match '/testing',   to: 'pages#testing',   via: 'get'
  match '/contacts',     to: 'contacts#new',             via: 'get'
  resources "contacts", only: [:new, :create]
  match '/events',   to: 'pages#mevents',   via: 'get'
  resources "events", only: [:new, :create]

  resources :photos
4

1 回答 1

0

这段代码有一些问题。

首先,路线不会将任何属性传递给您可以用来确定所需照片的方法。其次,您没有提交将在您的 photo_params 方法中提供照片参数的表单。幸运的是,您不需要它是动态的,因此您可以简单地删除该部分。最后,您不能简单地将所需的 id 添加到实例变量中的方法链中(一旦获得)。尝试调整你的代码看起来更像这样:

class PagesController < ApplicationController

  def landing
    @photo = Photo.find(1)
  end

end

<%= image_tag( @photo.image.thumb('150x185#').url ) if @photo.image_stored? %>
于 2014-08-29T05:52:48.240 回答