0

I'm building a Rails app which includes a map with markers (aka "plots" in the app). When a marker/plot is clicked an infobox appears next to the marker. I'm trying to add a feature where clicking an infobox renders a partial alongside the map with further details relevant to that specific plot.

I'm using Gmaps4Rails and the feature described above works fine when implemented with Google's standard Infowindows but if I try to use the InfoBox plugin, so I can have all the styling benefits of InfoBoxes, I get an error.

ActionController::UnknownFormat

Screen grab of rails error message here

Looking at the log it appears the request is being correctly sent as JS with Infowindows but for some reason when using the InfoBox plugin it is being sent as HTML.

Log message with Infowindow is this...

Started GET "/plots/1/plotdetails" for ::1 at 2016-05-21 13:50:02 +0100 Processing by PlotsController#plotdetails as JS Parameters: {"id"=>"1"} Plot Load (0.3ms) SELECT "plots".* FROM "plots" WHERE "plots"."id" = $1 LIMIT 1 [["id", 1]] Rendered plots/_plotdetails.html.erb (0.1ms) Rendered plots/plotdetails.js.erb (4.0ms) Completed 200 OK in 17ms (Views: 14.4ms | ActiveRecord: 0.3ms)

Log message with InfoBox plugin is below...

Started GET "/plots/2/plotdetails" for ::1 at 2016-05-17 18:52:38 +0100 Processing by PlotsController#plotdetails as HTML Parameters: {"id"=>"2"} Plot Load (0.3ms) SELECT "plots".* FROM "plots" WHERE "plots"."id" = $1 LIMIT 1 [["id", 2]] Completed 406 Not Acceptable in 3ms (ActiveRecord: 0.3ms)

ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/plots_controller.rb:95:in `plotdetails'

I can't find a solution so am thinking about trying to implement this with JQuery AJAX instead but I can't help but feel this should be fixable. Any ideas would be much appreciated.

routes.rb

Rails.application.routes.draw do   
  resources :architects
  resources :plots do
    get :plotdetails, :on => :member
  end
  root 'plots#map'

plots_controller.rb (there is more code in the controller but this seems to be the relevant part)

  def plotdetails
    @plot = Plot.find(params[:id])
    respond_to do |format|
      format.js { render :layout => false }
    end
  end

views/plots/map.html.erb

<%= render 'partials/menu' %>

<div id="plotdetails"></div>

<div id="map"></div>

<script type="text/javascript">

  buildMap(<%=raw @hash.to_json %>);

</script>

views/plots/_infobox.html.erb

<%= link_to plotdetails_plot_path(:id => plot.id), :remote => true do %>
  <div id="infobox">
      <div id="infobox-img">
        <img src="<%= plot.img_link %>">
      </div>
      <div id="infobox-text">
        <h3><%= plot.title %></h3>
        <p>Architect: <%= plot.architect.name %></p>
        <p><%= plot.address %></p> 
      </div>
  </div>
<% end %>

views/plots/_plotdetails.html.erb

<div>
      <ul>
          <li>plot.title</li>
          <li>plot.details</li>
      </ul>
</div>

views/plots/plotdetails.js.erb

$( "#plotdetails" ).html( "<%= escape_javascript( render( :partial => "plotdetails", :locals => { :plot => @plot} ) ) %>" );
Gavin
  • 971
  • 6
  • 10
  • The request is probably being sent as HTML. Check the log. The cause is most likely a script error which is preventing the Rails UJS handler from intercepting the click. – max May 16 '16 at 22:30
  • Where do you install the click handler for the infobox? Can you add that code to your question? – Michael Gaskill May 17 '16 at 00:15
  • Wht version on rails are you currently using? – kajal ojha May 17 '16 at 04:17
  • @kajal Rails version is 4.2.6. @Michael - the click handler for rendering the partial when the infobox is clicked is the link_to in _infobox.html.erb. Is that what you mean? @Max - think you're right. I've updated the question with the log contents. Not sure how best to fix this? Tried adding :format => 'js' to the link_to like this `<%= link_to plotdetails_plot_path(:id => plot.id, :format => "js"), :remote => true do %>` but then got error "Template is missing". Not sure if that is step forward or backwards. – Gavin May 17 '16 at 18:03

1 Answers1

0

Since respond_to has been removed from rails 4.2 to a individual gem i will recommend you to use gem 'responders'

Please checkout the link given below for more details..

Why is respond_with being removed from rails 4.2 into it's own gem?

Community
  • 1
  • 1
kajal ojha
  • 1,248
  • 8
  • 20
  • Thanks for the suggestion. I added the gem 'responders' to the gemfile but still the same issue. I have had a very similar piece of code working in another app with rails 4.2.6 so have a feeling there is something else going on here too. – Gavin May 18 '16 at 08:07