mercredi 20 avril 2016

Unable to upload image in rails 2.1.2

I am starter of rails. I am practicing a project in Ajax on Rails Book and unable to upload the pictures.

Please check the code and help me pls.

show.rthml

<h2 id="name"><%= @album.name %></h2>

<% if logged_in? %>
  <div id="rename" style="display: none">
    <%= javascript_tag "$('name').addClassName('rollover')" %>
    <%= javascript_tag "$('name').onclick=function(){ $('name').toggle(); $('rename').toggle() }" %>
    <% remote_form_for :album, @album, 
         :url => album_url, 
         :html => { :method => :put }, 
         :update => 'name', 
         :before => "$('name').update('Saving...'); $('name').toggle(); $('rename').toggle()" do |f| %>
      <%= f.text_field :name %> <%= link_to_function "Cancel" do |page| page.toggle :name, :rename end %>
    <% end %>
  </div>
  <div id="upload_container">
    <% form_for :photo, Photo.new, 
         :url => photos_url(:album_id => @album), 
         :html => { :multipart => true, :target => "uploader", :id => "photo_upload" } do |f| %>
      <label for="photo_file"><%= image_tag "add", :class => 'icon' %> Add a photo:</label> 
      <%= f.file_field :file, :onchange => "Photo.upload();" %>
    <% end %>
    <div id="loading" style="display: none">Uploading...</div>
    <iframe src="/404.html" id="uploader" name="uploader"></iframe>
  </div>
<% end %>

<div id="photos"><%= render :partial => "photos/index" %></div>

<%= render :partial => "photos/show" %>

application.js

var Photo = {

    upload: function() {
        $('loading').show();
        $('photo_upload').submit();
    },

    finish: function(url) {
        new Ajax.Updater('photos', url, {method:'get', onComplete:function(){
            $('loading').hide();
            $('photo_upload').reset();
        }});
    },

    show: function(url) {
        $('photo').src = url;
        $('mask').show();
        $('photo-wrapper').visualEffect('appear', {duration:0.5});
    },

    hide: function() {
        $('mask').hide();
        $('photo-wrapper').visualEffect('fade', {duration:0.5});
    },

    currentIndex: function() {
        return this.urls().indexOf($('photo').src);
    },

    prev: function() {
        if(this.urls()[this.currentIndex()-1]) {
            this.show(this.urls()[this.currentIndex()-1])
        }
    },

    next: function() {
        if(this.urls()[this.currentIndex()+1]) {
            this.show(this.urls()[this.currentIndex()+1])
        }
    },

    urls: function() {
    if (!this.cached_urls) {
            this.cached_urls = $$('a.show').collect(function(el){
                return el.onclick.toString().match(/".*"/g)[0].replace(/"/g,'');
            });
        }
        return this.cached_urls;
    }

}

Photos Controller

class PhotosController < ApplicationController

  before_filter :require_login, :only => [ :create, :update, :destroy ]
  before_filter :find_album
  before_filter :find_photo, :only => [ :update, :destroy ]

  def index
    render :partial => "index"
  end

  # Renders HTML containing a JavaScript callback to finish the upload
  def create
    @photo = @album.photos.create params[:photo]
    render :layout => false                     #'plain'
  end

  %w(full thumb medium).each do |size|
    class_eval <<-END
      def #{size}
        find_photo
        send_data @photo.#{size}, :filename => "\#{@photo.id}.#{size}.jpg", :type => 'image/jpeg', :disposition => 'inline'
      end
      caches_page :#{size}
    END
  end

  def update
    @photo.update_attributes :name => params[:name]
    render :update do |page|
      page["#{@photo.id}_name"].replace_html @photo.name
    end
  end

  def destroy
    @photo.destroy
    render :update do |page|
      page[:photos].update render(:partial => "index")
    end
  end

  private

    def find_album() @album = Album.find params[:album_id] end
    def find_photo() @photo = @album.photos.find params[:id] end

end

Photo Model

class Photo < ActiveRecord::Base
    require 'acts_as_list'

  belongs_to :album
  acts_as_list :scope => :album

  def file= file
    with_image file.read do |img|
      self.width  = img.columns
      self.height = img.rows
      write_attribute 'file', img.to_blob
    end
  end

  def full() file end

  def thumb
    with_image do |image|
      #geometry_string = (1 > (height.to_f / width.to_f)) ? "x100" : "100"
      geo = (1 > (height.to_f / width.to_f)) ? "x100" : "100"
        image = image.change_geometry(geo) do |cols, rows, img|
          img.resize!(cols, rows)
        end
        image = image.crop(Magick::CenterGravity, 100, 100)
        image.profile!('*', nil)
        return image.to_blob { self.format='JPG'; self.quality = 60 }
    end
  end

  def medium
    with_image do |img|
      maxw, maxh = 640, 480
      newratio = maxw.to_f / maxh.to_f
      w, h = img.columns,  img.rows
      oldratio = w.to_f / h.to_f
      scaleratio = oldratio > newratio ? maxw.to_f / w : maxh.to_f / h
      return img.resize(scaleratio).to_blob do 
        self.format='JPG'; self.quality = 60
      end
    end
  end

  private

    def with_image file=nil
      data = Base64.b64encode(file || self.file)
      img = Magick::Image::read_inline(data).first
      yield img
      img = nil
      GC.start
    end

end

The error is

NoMethodError in Albums#show Showing albums/show.rhtml where line #17 raised: undefined method `photos_url' for # Extracted source (around line #17):

Please help me. why Rails is saying photos_url is not defined. photos_url must generate url for photo.

Aucun commentaire:

Enregistrer un commentaire