سؤال

I've been beating my head against the keyboard for about a day over this one. I'm a bit of a noob, but I've done some research and can't seem to find the solution to this one.

I'm using Rails 4.0.2, Ruby 2.0.0p0, Bootstrap 2.3.2, Jcrop 0.9.12 , Rmagick 2.13.2

I'm trying to implement Jcrop in my app and keep getting the following error:

NoMethodError in PeopleController#update
undefined method `crop_x' for #<Person:0x007fc7a531d160>

I've looked at the Railscast on implementing Jcrop and it looks like I have things right.

I've also found some pages like this one that say there is a problem with a conflict between some CSS in Jcrop and Bootstrap. I tried adding the recommended code and that didn't fix it. So I looked at the jquery.Jcrop.css file and noticed that the fix is already included so I removed it from my css file.

Here is my photoUploader.rb where I have the crop code. The line I've marked with ** is the code highlighted the error when the page tries to load:

def crop
 **if model.crop_x.present?**
   resize_to_limit(600, 600)
   manipulate! do |img|
    x = model.crop_x.to_i
    y = model.crop_y.to_i
    w = model.crop_w.to_i
    h = model.crop_h.to_i
    img.crop!(x, y, w, h)
  end
 end
end

Here is my controller for updating a record:

def update
 respond_to do |format|
  if @person.update(person_params)
    if params[:person][:photo].present?
      render :crop
    else
      redirect_to @person, notice: "Successfully created person."
    end
    format.html { redirect_to @person, notice: 'Person was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @person.errors, status: :unprocessable_entity }
  end
 end
end

Here is my controller code with the strong parameters implemented:

    def person_params
  params.require(:person).permit(:fname, :mname, :lname, :company, :department, :title, :manager, :direct_report, :ntid, :work_phone, :mobile, :email, :office, :address, :city, :state, :zipcode, :country, :suite, :column, :fax, :assistant, :photo, :crop_x, :crop_y, :crop_w, :crop_h)
end

Here is the relevant code from my Person model:

mount_uploader :photo, PhotoUploader

after_update :crop_photo

def crop_photo
  photo.recreate_versions! if crop_x.present?
end

I'm completely stumped. Any assistance is greatly appreciated.

هل كانت مفيدة؟

المحلول

Are you used attr_accessor in your model like this

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top