0

I have a relation has_and_belongs_to_many with places and events. When i create a event dont save the places parameters but yes the others parameters. However, when i want to edit a event, this dont take the paremeters saved. (image) ¿what is the problem with the parameters and why dont save the ralation? Thanks

Event Model

cla
  has_and_belongs_to_many :users 
  has_and_belongs_to_many :sponsors 
  has_and_belongs_to_many :category 
  has_and_belongs_to_many :places
    accepts_nested_attributes_for :places
    
end

Place Model

 has_and_belongs_to_many :events  
  has_one :scores 
  has_and_belongs_to_many :categorys 
 
   accepts_nested_attributes_for :events

Events Controller

class EventsController < ApplicationController
  
  before_action :events_params, only: [:create, :edit]




  def index
   @event = Event.all
  end

 def show
    @event = Event.find(get_event)

  end

  def new 
   @event = Event.new
  end

def create
    @event = Event.new(events_params)

    if @event.save
      redirect_to events_path, notice: "Almacenado"
    else
      render "new", notice: "Danm"
    end
  end

def edit
    params_filtered = Event.find(events_params)
    @event = Event.find(get_event)
    if @event.update(params_filtered)
      redirect_to events_path
    else
      render 'new'
    end
  end

  def destroy
    @event = Event.find(get_event)
    @event.destroy
    redirect_to events_path
  end

  private
  def events_params
    params.require(:event).permit(:website,:city,:whybuy,:moendreinfo,:title,:description, :datestart, :dateend, :hourstart, :hourend, :price ,:avatar, :background, places_atributtes: []) 
  end

 def get_event
    @event = Event.find(params[:id])
  end




end

Routes

Rails.application.routes.draw do
  
  root 'welcome#index'

  resources :places do
  resources :events
  end 

 resources :events
 get "events/:id" => "events#show"
 get "events/:id/edit" => "events#edit"
  
  devise_for :users, :controllers => { registrations: 'registrations' }

  devise_scope :users do
  get "profile_user/:id" => "users_profiles#profile_user"
  get "usersall" => "users_profiles#usersall"
  end

end

Link of bug error

i.stack.imgur.com/Snl2m.png

Form Event new

<html>
<head>
 
   <title></title>
  </head>
  <body id="newevent">
   <h3> Crear Evento </h3>
    <div  class="container-fluid">
        <div  class="row">
          <div class="col-xs-12 col-sm-12 col-md-12  ">

           
              <di>
                    <div id="divnewevent" >

                        <%=form_for @event do |eve|%>
                         <%= eve.label :title, "Titulo del evento" %></br>
                          <%= eve.text_field :title,  class:"form-control", placeholder:"Stingi Fest Back Color", autofocus: true%></br>
                         <%= eve.label :description, "Descripción" %></br>
                         <%= eve.text_field :description,  class:"form-control", placeholder:"No debe superar los 140 caracteres", autofocus: true%></br>
                         
                          <%= eve.collection_select(:place_ids, Place.all, :id, :name, {prompt: true}, {multiple: true, class: "input-lg"}) %>
                          
                          <%= eve.label :datestart, "Fecha de apertura" %></br>
                          <%= eve.date_select :datestart, class:"form-control", placeholder:"Fecha de apertura o día del evento", autofocus: true %></br>

                         <%= eve.label :dateend, "Fecha de cierre" %></br>
                         <%= eve.date_select :dateend,  class:"form-control", placeholder:"Fecha de cierre o día del evento", autofocus: true %></br>
                         <%= eve.label :hourstart, "Hora inicio" %></br>
                         <%= eve.time_select :hourstart,  class:"form-control", placeholder:"Hora de inicio", autofocus: true %> </br>
                         
                         <%= eve.label :hourend, "Hora cierre" %></br>
                         <%= eve.time_select :hourend, class:"form-control", placeholder:"Hora de cierre", autofocus: true %></br>

                          <%= eve.label :website, "website" %></br>
                          <%= eve.text_field :website, class:"form-control", placeholder:"www.cosito.com", autofocus: true  %></br>

                          <%= eve.label :city, "ciudad" %></br>
                          <%= eve.text_field :city, class:"form-control", placeholder:"Ciudad,co", autofocus: true %></br>

                          <%= eve.label :price, "Costo" %>
                          <%= eve.text_field :price, class:"form-control", placeholder:"Costo de entrada", autofocus: true %> </br>
                       
                          
                          <%= eve.label :whybuy, "Por qué asistir o comprar y dónde" %></br>
                          <%= eve.text_field :whybuy, class:"form-control", placeholder:"Plus de insentivo y donde comprar", autofocus: true %></br>
                          <%= eve.label :moreinfo, "más info" %></br>
                          <%= eve.text_field :moreinfo, class:"form-control", placeholder:"Mayor información", autofocus: true %></br>
                          <%= eve.label :avatar, "Foto de perfil" %>
                           <%= eve.file_field :avatar %>
                           <%= eve.label :background, "Foto de fondo" %>
                           <%= eve.file_field :background %>



                          <%= eve.submit %>
                         <%end%>

                            </div>                  
                   </div>
                
              </div>
            </div>
        </div>
    
  </body>
</html>

1 Answers1

0

I have too low rep to comment.

Please show the view of the form . It appears as if the parameters doesn't live up to your

params.require(:event) 

Edit:

I think you do not use your event_params the right way, you should call this function only when you want to check the given parameters and look for your permitted attributes.

You should remove the

before_action :event_params

But add:

before_action :get_event, except: [:new, :index, :create]

This will make sure that you will set the current event in the @event-variable before show, update, destroy.

I think your culprit could be that you do not have an update-method, you appear do this in your edit-method as well.

edit is the action that loads the page where you can edit your event. updateis the action that you send to from your form. This is where you will use @event.update(...).

So your edit and update can look like:

def edit
end

def update
  if @event.update(event_params)
    redirect_to @event, notice: 'Event was updated'
  else
    render action: :edit
  end
end

(If you added the before_action :get_event).

davidwessman
  • 1,170
  • 8
  • 27