Hello everyone, pass params of a link_to to the instance of a controller action and thus save in db

Viewed 39

Good afternoon, I am trying to pass params from a link_to to the instance of a controller action and thus save that data in the database, but for some reason they do not pass. I appreciate some help, and good morning ..

This is the controller of reservas

    
  def index
    @reservas = Reserva.all
  end

  def show
    @reserva = Reserva.find(params[:id])
  end

  def new
    @reserva = Reserva.new
  end
  
  def create
  end
  
  def cupoClase
    @clase = Clase.find(params[:id])
    @reserva = @clase.reservas.new(params[:reserva])
    @reserva.idUsuario = current_usuario.id
    @reserva.clase_id = params[:id].to_i
    @reserva.fecha = params[:fecha]
  
    respond_to do |format|
    if @reserva.save
      format.html { redirect_to @reserva, notice: 'Clase reservada' }
      format.json { render action: 'index', status: :created, location: @reserva }
    else
      format.html { render action: 'index' }
      format.json { render json: @reserva.errors, status: :unprocessable_entity }
    end
  end
  
  end

  private
  def reserva_params
    params.require(:reserva).permit(:idUsuario, :clase_id, :fecha)
  end

  def set_reserva
    @reserva = Reserva.find(params[:id])
  end
end ```

This is the index html.erb, and has the link_to of the params

<% @clases.each do |clas| %>

<tr>
  <td><%= clas.nombre %></td>
  <td><%= clas.fecha %></td>
  <td><%= clas.cupos %></td>
  <td><%= link_to 'Reservar', {:controller => 'reservas', :action => 'cupoClase', :id => clas.id, :fecha => clas.fecha } %></td>
  <% if can? :destroy, @clase %>
  <td><%= link_to 'Eliminar', clase_path(clas), method: :delete, data: { confirm: "Eliminar?" } %></td> |
  <% end %>
</tr>

<% end %>

This is the routes..

``` Rails.application.routes.draw do
 devise_for :usuarios, :controllers => { registrations: 'registrations' }
 root to: 'home#index'
 
 resources :clases do
  resources :reservas
 end
 
 get '/clases', to: 'clases#index'
 get '/reservas/:fecha/:id', to: 'reservas#cupoClase'
 
end
0 Answers
Related