ruby on rails - Dealing with 2 models in 1 view -


i have 2 models i'm trying interact in 1 view. first model room , second availability. 1 room has many availabilities.

on 1 rooms page, render out availabilities through partial this:

<% @room.availabilities.where("booking_id null").order("start_time").each_with_index |a|%> 

beside each availability have button delete , update. delete worked out fine since in loop this.

<%= link_to("delete", availability_path(a.id), method: :delete,  remote: true) %> 

but i'm having trouble edit. i'm trying through modal doesn't have access 'a' variable loop. i'm not sure how pass in unique availability form

button:

<!-- edit button --> <button type="button" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#editavailability"><i class='fa fa-pencil'></i></button>  <!-- edit availability form --> <%= simple_form_for @facility.availabilities.find(???), method: :put, remote: true |f| %> 

you should able ajax. have app has modal dropdown lets me toggle pieces of equipment in or out of service button in dropdown. can use route points towards availabilities controller renders form in modal. mine simple in toggles, don't see why can't use form. move query out of view , make helper gives results of query in variable.

i can provide more detail need see lot more of current code. if can post controller , of view code modal. don't know understand why don't have access variable need in modal? if modal has ajax call should able populate data available controllers.

edit

take @ this: https://coderwall.com/p/ej0mhg/open-a-rails-form-with-twitter-bootstrap-modals . sure , read links @ end of article, has stackoverflow examples spot on. i'm thinking link_to way go:

<%= link_to 'update, availabilities_edit_path(a.id), {:remote => true, 'data-controls-modal' =>  "modal-window", 'data-backdrop' => true, 'data-keyboard' => true} %> 

this should open modal , ask availabilities#edit controller js response. make sure have edit action, don't see 1 in controller:

availabilities.rb    def edit     @availability = availability.find_by(id: params[:id])     respond_to |format|     format.html     format.js   end 

so js call cause in /views/availabilities/ folder file called edit.js.erb content like:

$('#editavailability').html('<%= escape_javascript(render :partial => 'editform') %>'); 

in edit form you have @availability instance variable use in form:

<%= simple_form_for @availability, method: :put, remote: true |f| %> ... 

so modal gets built using ajax lookup returns form built using needed instance variable. i'm putting stuff i've written , other stuff i've read, there errors , tweaking code working. let me know how far gets you.


Comments