ruby on rails - Nested Model not saving on Initial create -


i have double nested model "days" not save save when edit record afterward. believe due how creating second model "schedule" in first models controller

my objects , associations:

event has_one schedule

schedule belongs_to_event has_many days

days belongs_to schedule

i want create schedule automatically along event, there way or bad practice , should have attempted in different manner?

   class eventscontroller < applicationcontroller    before_action :set_event, only: [:show, :edit, :update, :destroy]   before_action :authenticate_user!, except: [ :show ]    def index     @events = current_user.events.all   end    def show   end    def new     @event = current_user.events.build     @schedule = @event.build_schedule(event_id: @event.id, user_id: current_user.id, )    end    def edit     if @event.user_id != current_user.id       flash[:error] = "not allowed"       redirect_to root_path     else       @schedule = @event.schedule     end    end    # post /events   # post /events.json   def create     @event = current_user.events.create(event_params)     @schedule = @event.create_schedule(event_id: @event.id, user_id: current_user.id)        respond_to |format|       if @event.save         format.html { redirect_to @event, notice: 'event created.' }         format.json { render :show, status: :created, location: @event }       else         format.html { render :new }         format.json { render json: @event.errors, status: :unprocessable_entity }       end     end   end    def update     if @event.user_id == current_user.id       respond_to |format|         # puts "call sanitize_url"         # sanitize_url         if @event.update(event_params)           format.html { redirect_to @event, notice: 'event updated.' }           format.json { render :show, status: :ok, location: @event }         else           format.html { render :edit }           format.json { render json: @event.errors, status: :unprocessable_entity }         end       end     else       flash[:error] = "not allowed"       redirect_to root_path     end   end    # delete /events/1   # delete /events/1.json   def destroy     if @event.user_id == current_user.id       @event.destroy       respond_to |format|         format.html { redirect_to events_url, notice: 'event destroyed.' }         format.json { head :no_content }       end     else       flash[:error] = "not allowed"       redirect_to root_path     end   end    private     # use callbacks share common setup or constraints between actions.     def set_event       @event = event.find(params[:id])     end      def event_params       params.require(:event).permit(         :name, :description,          links_attributes: [:id, :label, :url, :_destroy],          schedule_attributes: [:id, :_destroy, days_attributes: [:id, :date, :_destroy]]         )     end   end 

form code: (i using cocoon nested forms.)

<%= simple_form_for(@event) |f| %> <%= f.error_notification %>  <div class="form-inputs">     <%= f.input :name %>     <%= f.input :description %>     <hr> </div>  <div>     <button type="button" class="btn schedbtn btn-default btn-block">event schedule</button>      <div class="collapse">         <br>         <%= f.simple_fields_for :schedule |schedule| %>             <%= render 'schedule_fields', f: schedule %>         <% end %>     </div>     <hr> </div>  <script>     $(document).ready(function(){         $(".schedbtn").click(function(){             $(".collapse").collapse('toggle');         });     }); </script>  <div class="nested-fields">     <%= f.simple_fields_for :links |link| %>         <%= render 'link_fields', f: link %>     <% end %> </div> <div class="links">     <%= link_to_add_association 'add link', f, :links, class: "btn btn-block btn-default add-buton" %> </div>  <div class="form-actions" style="padding: 0px 0px 15px 0px;">     <hr>     <%= f.button :submit %>      <%= link_to 'back', events_path, class: "btn btn-default", style: "margin-left: 10px;" %> </div> 

schedule partial:

<p><%= @event.schedule.id %></p> <p><%= @schedule %><%= @schedule.id %></p> <div class="links">     <%= link_to_add_association 'add day', f, :days, class: "btn btn-block btn-default add-buton" %> </div> <div class=" nested-fields">     <%= f.simple_fields_for :days |day| %>         <%= render 'day_fields', f: day %>     <% end %> </div> 

days partial:

<div class="nested-fields"> <%= f.input :date, input_html: { class: "form-input form-control" } %> <%= link_to_remove_association "remove", f, class: "form-button btn btn-default" %> <hr> 

here server output during initial commit:

sql (0.5ms)  insert "schedules" ("user_id", "created_at", "updated_at") values (?, ?, ?)  [["user_id", 1], ["created_at", "2015-06-04 02:34:38.724966"], ["updated_at", "2015-06-04 02:34:38.724966"]]    (4.5ms)  commit transaction    (0.1ms)  begin transaction  day load (0.2ms)  select "days".* "days" "days"."schedule_id" = ?  [["schedule_id", 31]]   sql (0.4ms)  delete "days" "days"."id" = ?  [["id", 24]]   sql (0.2ms)  delete "schedules" "schedules"."id" = ?  [["id", 31]]    (1.5ms)  commit transaction 

as can see not saving parameters days reason.

so figured out removing line

@schedule = @event.create_schedule(event_id: @event.id, user_id: current_user.id) 

from create method fixes problem. guess since saving using @event params , nested form create method interrupted that.

obviously still have lot learn rails.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -