ruby on rails - How to access associated object id in ajax loaded form -
using rails 3.2, how can access id of associated object within embedded form loaded ajax?
for example, have object called hotels. each hotel has_many rooms. when edit hotel add new rooms hotel using ajax loaded new rooms form on hotel edit page.
i started creating ajax using jquery on rooms index page. works great there, don't have hotel id, present hotel selection dropdown box. now, add new room directly on hotel edit page. don't understand how pass hotel id (@hotel.id) new ajax loaded room _form.html.erb partial. on _form.html.erb partial, have:
<% if defined?(@hotel.id) %> <%= f.hidden_field :hotel_id, :value => @hotel.id %> <% else %> <%= f.collection_select(:hotel_id, hotel.all, :id, :name) %> <% end %>
on hotel edit page, have link add new room:
<%= link_to 'add room', new_room_path, :remote=>true, :class => 'new_room' %>
and in rooms_controller.rb have:
# /rooms/new # /rooms/new.json def new @room = room.new render :partial => 'form', :locals => { :room => @room } end
how can associated @hotel.id inside ajax loaded new room _form.html.erb partial?
Comments
Post a Comment