Default scaffold routes and controllers

2

I have a little doubt about the drivers and the routes. I am developing an image editing system and the only thing I need is to redirect to each route, that is, the user modifies the image in each system view, first the image upload view, then goes directly to the view of clipping, then to the edit view, but when it reaches the editing path called "et", it is not responding with the method specified in the path, but is responding by the update method of the scaffold, and is ignoring its own method.

I have the following specified routes:

 get '/attachments/:id/et', to: 'attachments#et', as: 'et'       #esta ruta redirige a la vista et con la imagen como parametro 

 resources :attachments

 patch '/attachments/:id', to: 'attachments#et_update'
 put '/attachments/:id', to: 'attachments#et_update'

At the last two I am asking you to respond to the et_update method that is defined in the controller, but when the system uses the methods, use the update of the scaffold instead of "et_update".

my controller

class AttachmentsController < ApplicationController
  before_action :set_attachment, only: [:show, :edit, :update, :destroy, :et, :et_update, :texto, :texto_update]

  # GET /attachments
  # GET /attachments.json
  def index
    @attachments = Attachment.all
  end

  # GET /attachments/1
  # GET /attachments/1.json
  def show
  end

  # GET /attachments/new
  def new
    @attachment = Attachment.new
  end

  # GET /attachments/1/edit
  def edit

  end

  # POST /attachments
  # POST /attachments.json
  def create
    @attachment = Attachment.new(attachment_params)

    #respond_to do |format|
      if @attachment.save
        if params[:attachment][:image].present?
          render :crop
        else
          format.html { redirect_to @attachment, notice: 'Attachment was successfully created.' }
          format.json { render :show, status: :created, location: @attachment }
        end
      else
        format.html { render :new }
        format.json { render json: @attachment.errors, status: :unprocessable_entity }
      end
    #end
  end

  # PATCH/PUT /attachments/1
  # PATCH/PUT /attachments/1.json

def et

end

#edit es el que va a verse
#update recibe los datos para guardarlos, aqui en el scaffold

def et_update

    if params[:base64].present?
                   data =  params[:base64]
                   image_data = Base64.decode64(data['data:image/png;base64,'.length .. -1])
                   File.open("#{Rails.root}/public#{@attachment.image.url.to_s}", 'wb') do |f|
                   f.write image_data
          redirect_to texto_path, notice:"successfully updatesd attachment"

                 end
                  @attachment.image.recreate_versions! if @attachment.image.present?

                  end





end



  def update
    #respond_to do |format|
      if @attachment.update(attachment_params)

          redirect_to et_path, notice:"successfully updated attachment"

          #format.html { redirect_to @attachment, notice: 'Attachment was successfully updated.' }
          #format.json { render :show, status: :ok, location: @attachment }


      else
        format.html { render :edit }
        format.json { render json: @attachment.errors, status: :unprocessable_entity }
      end
    #end
  end

  # DELETE /attachments/1
  # DELETE /attachments/1.json
  def destroy
    @attachment.destroy
    respond_to do |format|
      format.html { redirect_to attachments_url, notice: 'Attachment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_attachment
      @attachment = Attachment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def attachment_params
      params.require(:attachment).permit(:image, :name, :crop_x, :crop_y, :crop_w, :crop_h, :data, :image_data, :base64, :@ac2, :ac2)
    end
end

Why does such a strange behavior happen like this?

    
asked by LuisC 11.03.2016 в 22:26
source

1 answer

2

Your problem is that surely the update path is overwriting the one you indicate with et_update.

Edited

Another possibility is that the form route is defined by default <%= form_for @objeto , so when you validate the form redirects to edit . You can change this behavior in the following way: <%= form_for @objeto, :url => {:action => 'et_update'} %>

In any case, you can see your routes by running the rake routes command in a local server window or, if not, trying to display a non-existent page http://localhost:3000/ruta_no_valida .

If you do not use the update method of your controller, why do not you put the update code of your model in it instead of creating a new action and complicating your code?

    
answered by 14.03.2016 / 14:06
source