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} ) ) %>" );
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?
Related
I'm trying to make a search kick results page. However when i click search the console states i've got the correct id but the results page isnt opening.
I have this for my controller
def search
#events = Newevent.search params[:search]
render 'events/results'
end
I also have this as my search form
<div class="input-group input-group-lg">
<%= text_field_tag :search, params[:search], class: "form-control", autocomplete: "off" %>
<div class="input-group-btn">
<%= submit_tag "Search", class: "btn btn-primary" %>
</div>
</div>
<% end %>
And this is inside the results html.erb
<% #events.each do |event| %>
<h1>Test</h1>
<%= event.eventname %>
<% end %>
Heres what the console states when i start the search
Started GET "/search_events?utf8=%E2%9C%93&search=capital&commit=Search" for ::1 at 2015-11-21 15:18:33 +0000
Processing by NeweventsController#search as JS
Parameters: {"utf8"=>"✓", "search"=>"capital", "commit"=>"Search"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Newevent Search (9.8ms) curl http://localhost:9200/newevents_development/_search?pretty -d '{"query":{"dis_max":{"queries":[{"match":{"_all":{"query":"capital","operator":"and","boost":10,"analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"capital","operator":"and","boost":10,"analyzer":"searchkick_search2"}}},{"match":{"_all":{"query":"capital","operator":"and","boost":1,"analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"_all":{"query":"capital","operator":"and","boost":1,"analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"size":100000,"from":0,"fields":[]}'
Newevent Load (0.5ms) SELECT "newevents".* FROM "newevents" WHERE "newevents"."id" = 20
Rendered events/results.html.erb within layouts/application (1.6ms)
Rendered layouts/_navigation.html.erb (0.1ms)
Rendered layouts/_messages.html.erb (0.1ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 93ms (Views: 79.5ms | Searchkick: 9.8ms | ActiveRecord: 1.0ms)
Base on your server's log, I think you setup your search form as a remote form (you will see you have remote: true in your form_for tag)
Processing by NeweventsController#search as JS
Do you see text: as JS in the above line. That's because you submit a remote form. That means when you submit your form, the form will call ajax to server wait for server to return a js code to run on client side.
To solve your problem, just remove remote: true in your form_for tag, and everything will be ok ;)
I use Jvectormap into my Rails 3 app.
When I click on it, I want to hide the map and visualize a partial with information about the selected country.
I handle the map click and I send a GET req to the server.
users.js.coffee.erb
$ ->
$("#world-map").vectorMap
onRegionClick: (event, code) ->
$('#world-map').hide(1000)
$.get('/users/statistics', {code: code});
$('#statistics').show(1000)
users_controller.rb
def statistics
#country = Country.find_by_two_letter_code((params[:code]).downcase)
render :nothing => true
end
this is the response
Started GET "/users/statistics?code=ET" for 127.0.0.1 at 2013-06-02 17:54:49 +0200
Processing by UsersController#statistics as /
Parameters: {"code"=>"ET"}
Country Load (0.2ms) SELECT "countries".* FROM "countries" WHERE
"countries"."two_letter_code" = 'et' LIMIT 1
Rendered text template (0.0ms)
Completed 200 OK in 2ms (Views: 0.6ms | ActiveRecord: 0.2ms)
and the view
show.html.erb
<p> <div id="world-map" style="width: 620px; height: 300px"></div>
<div id="statistics" style="width: 620px; height: 300px; display:none;">
<section>
<%= render partial:'statistics', :locals => {:country => #country} %>
</section>
</div>
</p>
_statistics.html.erb
<%= link_to '<i class="icon-remove"></i>'.html_safe%>
<%=country%>
<%=#country%>
Now, everything works, but the partial doesn't visualize the country value.
I don't undestand if I have to re-render the partial after the ajax get request, and how.
I've tried to change the controller in another way, but the behavior is the same.
def statistics
#country = Country.find_by_two_letter_code((params[:code]).downcase)
render 'users/_statistics'
end
i think that since you don't need a page refresh and just need to display the details the country, you can use ajax.
show.html
<p> <div id="world-map" style="width: 620px; height: 300px"></div>
<div id="statistics" style="width: 620px; height: 300px; display:none;">
<section>
<div id="place_partial_here"></div>
</section>
</div>
</p>
in the controller
def statistics
#country = Country.find_by_two_letter_code((params[:code]).downcase)
respond_to do |format|
format.js
end
end
in statistics.js.haml #i am comfortable with haml.
$('#place_partial_here').replaceWith("#{escape_javascript(render partial: 'country_data', locals: {country: #country})}");
in the _country.html.haml
#place_partial_here #id is denoted in haml using hash
= country.name #display country name
= country.code #display code
the partial must have the view code 'inside' the id 'place_partial_here' so that it can be used in further ajax calls
have you tried this
render :partial => "statistics", :collection => #country
I am using Carrierwave to upload files. On my development machine running OS X 10.8 I am able to upload the file and download the file as expected. Tailing the development log I see:
Started GET "/resources/download_file/11" for 127.0.0.1 at 2013-03-24 06:29:33 -0400
Processing by ResourcesController#download_file as HTML
Parameters: {"id"=>"11"}
Resource Load (0.2ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT 1 [["id", "11"]]
Sent file /Users/scervera/rails_apps/mdn/public/uploads/resource/document/11/MyDocument.pdf (0.1ms)
Completed 200 OK in 2ms (ActiveRecord: 0.2ms)
After deploying with Capistrano onto my Ubuntu 12.04 server VM, I am able to upload the file (and verify it is physically on the server), however, when I click on the link to download the file I receive a message that says:
"The file “MyDocument.pdf” could not be opened because it is empty."
Tailing the production.log shows this:
Started GET "/resources/download_file/1" for 192.168.0.90 at 2013-03-24 06:31:12 -0400
Processing by ResourcesController#download_file as HTML
Parameters: {"id"=>"1"}
Sent file /var/mdnapp/releases/20130322184251/public/uploads/resource/document/1/MyDocument.pdf (0.1ms)
Completed 200 OK in 2ms (ActiveRecord: 0.1ms)
Note that this sql line is missing:
Resource Load (0.2ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ?
When I tried to open the file, it is indeed empty.
Also, I have tried adding the following to my deploy.rb, but while this is helpful, did not solve this particular problem:
set :shared_children, shared_children + %w{public/uploads}
Additional Details
I have a resources_controller with a method called download_file. See below:
def download_file
#resource = Resource.find(params[:id])
send_file(#resource.document.path,
:type => 'application/pdf',
:disposition => 'attachment',
:url_based_filename => true)
end
In my view I have a link tag that calls this method. See view code below:
<% if #resources.empty? %>
<p>There are no resources available at this time.</p>
<% else %>
<% #resources.each do |resource| %>
<div class="resource_wrap clearfix">
<h1><%= resource.title.html_safe %></h1><h2><%= resource.subtitle.html_safe %></h2>
<p><%= image_tag resource.image_url(:wallet).to_s %><%= resource.description.html_safe %></p>
<div id="attachments">
<% if resource.document? %>
<p>Get the resource:<%= link_to 'Download File', :action => 'download_file', :id => resource.id %></p>
<% end %>
<% if resource.video? %>
<p>Get the video:<%= link_to 'Download Video', :action => 'download_video', :id => resource.id %></p>
<% end %>
</div>
</div>
<% end %>
<% end %>
I'm running rails 3.2.11, ruby 1.9.3p194, carrierwave 0.8.0, rmagick 2.13.2
Please let me know if you need any additional details.
OK. I figured out the problem. I had to edit my config/environments/production.rb file and commented out the following line:
config.action_dispatch.x_sendfile_header = "X-Sendfile"
Spent a lot of hours researching this one. I hope someone else can benefit.
I'm trying to simply render a view using UJS and Rails 3, but it's not actually working. I've read many tutorials but can't seem to get this. Here are some code snippets:
index.js.erb
"<%= render user_path(#this_user) %>"
index.html
<%= form_tag users_path, :method => :get, :remote => true do %>
<%= submit_tag "Test", :name => nil, :class => "btn" %>
<% end %>
users_controller.rb
def index
#users = User.all
#this_user = User.find(1)
respond_to do |format|
format.html # index.html.erb
format.js
format.json { render json: #users }
end
end
Every time I click on "Test", I get the following output:
Started GET "/users?utf8=%E2%9C%93" for 127.0.0.1 at 2012-10-19
11:31:49 -0700 Processing by UsersController#index as JS Parameters:
{"utf8"=>"✓"} User Load (0.4ms) SELECT "users".* FROM "users"
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" =
$1 LIMIT 1 [["id", 1]] Rendered users/index.js.erb (1.8ms)
Completed 500 Internal Server Error in 6ms
ActionView::Template::Error (The partial name (/users/1) is not a
valid Ruby identifier; make sure your partial name starts with a
letter or underscore, and is followed by any combinations of letters,
numbers, or underscores.):
1: "<%= render user_path(#this_user) %>" app/views/users/index.js.erb:1:in
_app_views_users_index_js_erb__2145063269526192522_70203075823780'
app/controllers/users_controller.rb:9:inindex'
It appears it's looking for a partial; however I just want it to execute the view associated with the "show" action on the User controller. So a completely new page.
What am I doing wrong? First step is to render a completely new view, but I plan to render a partial view within the "show" page after I figure this out.
Thanks in advance for any help!
Look at index.js.erb:
<%= render user_path(#this_user) %>
That is telling Rails to render a partial with the name of the value of user_path(#this_user).
You view file index.js.erb needs to return JavaScript. So, the following would work:
alert('The Id of this User Object is: <%= #this_user.id %>');
In the future, you can render a partial (that contains JavaScript code) by doing the following:
$("#popup").html("<%= escape_javascript(render(:partial => 'show_sold_out_popup') %>");
I'm a newbye that is trying to do little things in Rails, at the moment I have created an app that build some links and I want to refresh the content of a Div when they are clicked.
In my application.html.erb I have the layout with the next head:
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
(I have read about change the second line for :defaults but if do that I get a 404 error defauls.js)
I have an index.html.erb that render a partial with this:
<%= render :partial => 'masvisitadas' %>
In my _masvisitadas.html.erb I have the code that generate the links:
<%= link_to solution.SolutionName, "/soluciones/despliega/" + solution.Id.to_s, :remote=>true %>
This generate the next html:
http://localhost:3000/soluciones/despliega/501D0000000QWp6IAG
In my controller I have the next def:
def despliega
respond_to do |format|
format.js {}
end
end
Finally I have created despliega.js.erb:
jQuery(function($) {
var html = "<%= escape_javascript(render('despliega')) %>";
$("#customTopLeft").prepend(html)
$("customTopLeft").html("<%= escape_javascript(render(:partial => 'despliega')) %>");
});
In the log of the web server when i click a link it seems all going well:
Started GET "/soluciones/despliega/501D0000000QXM0IAO" for 127.0.0.1 at 2012-08-
20 12:16:53 +0200
Processing by SolucionesController#despliega as JS
Parameters: {"id"=>"501D0000000QXM0IAO"}
Rendered soluciones/_despliega.html.erb (0.0ms)
Rendered soluciones/_despliega.html.erb (0.0ms)
Rendered soluciones/despliega.js.erb (15.6ms)
Completed 200 OK in 31ms (Views: 31.3ms | ActiveRecord: 0.0ms)
And in the firebug I can see the call with any error but nothing happends.
This is killing my head :) Thanks in advance for any help here.
Regards.
What's in _despliega.html.erb?
Try changing despliega.js.erb to this (and only this):
$('#customTopLeft').html("<%= escape_javascript(render :partial => 'despliega') %>");
Edit - oh, and make sure you have the jQuery libraries included ..