Variable params [: action] on model in RoR

0

I'm trying to work with the variable action in a model in rails, but it generates this error:

Any ideas ??? in advance, thanks !!!

Deputy controller

class MensajesController < ApplicationController

  before_action :set_mensaje, only: [:show, :alcance, :edit, :update, :grupo, :destroy]
  before_action :authenticate_user!
  before_action :cuota_sms

  def create
    @mensaje = Mensaje.new(mensaje_params) do |mensaje|
    mensaje.user = current_user
    mensaje.parent_id = params[:parent_id]
    end
    respond_to do |format|
      format.js
    end
  end

  def show
  end

  def alcance
    mensaje = {"mensajes_id_in_any"=>[params[:id].to_s]}
    @buscar = Contacto.search(mensaje)
    @enviado_por = 'xxx'
    @contactos = @buscar.result(distinct: true).order("numero")
  end

  def edit
  end

  def update
    respond_to do |format|
      if @mensaje.update(mensaje_params)
        format.html { redirect_to '/', notice: 'mensaje editado' }
      else
        format.html { render :edit }
        format.json { render json: @contacto.errors, status: :unprocessable_entity }
      end
    end
  end

  def grupo
    respond_to do |format|
      if @mensaje.update(mensaje_params)
        format.html { redirect_to mensaje_path, notice: 'Alcance actualizado' }
      else
        format.html { render :show }
        format.json { render json: @contacto.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @mensaje_id = params[:id]
    respond_to do |format|
      format.js
    end
  end

  private
  def set_mensaje
    @mensaje = Mensaje.find(params[:id])
  end

  def mensaje_params
    params.require(:mensaje).permit(:texto, :cantidad_contactos, grupo_ids:[], mensaje_ids:[])
  end

end

Attached full code of the model

class Mensaje < ActiveRecord::Base

  has_and_belongs_to_many :grupos
  has_and_belongs_to_many :contactos

  after_update   :actualizar_mensaje

  self.per_page = 20

  belongs_to :user

  validates :texto,
  presence: { message: "en blanco" },
  length: { maximum: 160, :message => "muy largo" }

  counter_culture :user
end

private
  def actualizar_mensaje
   puts ''''''''''''''''''''''''''''''''''''''''''''
   puts ''''''''''''''''''''''''''''''''''''''''''''
   puts "accion: #{params[:action]}"
   puts ''''''''''''''''''''''''''''''''''''''''''''
   puts ''''''''''''''''''''''''''''''''''''''''''''
    contador = 0 
    vector = []
    gm = GruposMensaje.where(mensaje_id: self.id)
    gm.each do |gml|
      grp = Grupo.find(gml.grupo_id)
      cg = ContactosGrupo.where(grupo_id: grp.id) 
      cg.each do |cgl|
        contacto = Contacto.find(cgl.contacto_id)
        vector[contador] = contacto.id
        contador += 1
      end
    end
    vector = vector.sort.uniq
    total = vector.length
    mensaje = Mensaje.find(self.id)
    mensaje.update_columns(cantidad_contactos: total)

    mensaje.contactos.delete_all
    vector.each do |id_contacto|
      ContactosMensaje.create( { "mensaje_id" => self.id, "contacto_id" => id_contacto })
    end
  end

Basically what I want is that when you call the update action on the controller, the after_update: update_message will be executed, but when you invoke the group action, do not run after_update: update_message.

    
asked by rrg1459 07.09.2017 в 04:44
source

2 answers

0

As far as I can see the error is because in the controller you do not have an action for that route, it would be nice if you raise the code of your controller.

I upload an example of a controller:

class TicketsController < ApplicationController

  before_action :set_ticket, only: [:show, :edit, :update, :destroy]
  respond_to :html, :json

  # GET /tickets/1
  # GET /tickets/1.json
  def show

  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def ticket_params
      params.require(:ticket).permit(:subtotal, :total, :pay_with, :change, :status)
    end
end

As you can see before each action, show, edit, update and drestroy, set the ticket @ticket = Ticket.find(params[:id]) , in this case it will send you with the route link should send the ticket properties with id = 1, as long as it exists.

    
answered by 07.09.2017 в 15:57
0

The variable params is not accessible at the model level, only from the controller or the view.

Update

There are several ways to do what you require. One, which I think is the simplest, is to simply remove the after_update , leave your method actualizar_mensaje as public and call the method from the controller when you need it:

  def update
    respond_to do |format|
      if @mensaje.update(mensaje_params)
         # Aquí es requerido, así que lo llamas
        @mensaje.actualizar_mensaje
        format.html { redirect_to '/', notice: 'mensaje editado' }
      else
        format.html { render :edit }
        format.json { render json: @contacto.errors, status: :unprocessable_entity }
      end
    end
  end

  def grupo
    respond_to do |format|
      if @mensaje.update(mensaje_params)
         # Aquí no, entonces no haces nada
        format.html { redirect_to mensaje_path, notice: 'Alcance actualizado' }
      else
        format.html { render :show }
        format.json { render json: @contacto.errors, status: :unprocessable_entity }
      end
    end
  end

If you do not want to add logic to the controller, you could create two methods update1 and update2 in the model and both call update, but one call the method actualizar_mensaje and the other one does not.

Another option, which could be more appropriate, is to create in your model a attr_accessor with a boolean that defines whether the message is group or not. Something like:

class Mensaje < ActiveRecord::Base

  attr_accessor :es_grupo

  after_update :actualizar_mensaje, unless: :es_grupo?

For this, you should send that variable, either by adding it from your controller with something like @mensaje.update(mensaje_params.merge(grupo: true)) or, better yet, from the form of the view.

Point aside, I think that your action grupo in the controller does not make sense that it exists if you do the same as update except for calling the actualizar_mensaje method, which, as I commented, you can control perfectly indicating from the view if you need or not call it.

    
answered by 07.09.2017 в 04:52