I'm trying to implement a voting system kind of like the one here on Stack Overflow. (Using Rails 3) I want the voting to be done without a page reload, so I have this code
link_to("/tags/#{tag.id}/upVote", :remote => true )
And so in my /views/tags directory I have a file called _upVote.js.erb that I thought would be called when this link is clicked, but it is not. It is trying to process upVote as HTML and this is the error I get
Missing template tags/upVote with {:formats=>[:html]
Also, here is what I have in my routes file
match "tags/:id/upVote" => "tags#upVote"
Any ideas how I can get this to work?
If you got this error message in a blank new page, that means that your remote call does not work and the call wasn't made via an Ajax request. You need to check that your layout correctly loads jQuery and the jQuery Rails connector available here : http://github.com/rails/jquery-ujs
Then use Firefox+Firebug to check that the call is really an Ajax call.
I had this same problem.
To resolve the problem, I followed
https://launchschool.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails
Rails 4 rendering a partial with ajax, jquery, :remote => true, and respond_to
And finally, I had to
Require both jquery and jquery_ujs were in the application.js manifest.
//= require jquery
//= require jquery_ujs
After all that, Rails ajax started working for me.
See this post for a solution:
Rails form_for :remote=>true is not calling js method
When changing the rails environment to JQuery, you may accidentally lose your jquery-ujs file.
type something like inside your rails application root:
rails g jquery:install
And then, inside your application.html.erb add the line
<%= javascript_include_tag :defaults %>
or explicitly (do not forget to include your jquery separately):
<%= javascript_include_tag :rails, :application %>
[EDIT: for Rails 3.1 or greater using the asset pipeline]
Use the gem jquery-rails (as mentioned above) and add the following lines to the app/assets/javascripts/application.js (if they are not there already) :
//= require jquery
//= require jquery_ujs
Hope this helps!
Related
I have a form in my application as this
<%= form_tag(studios_path, method: :get, id: "studios_filter", remote: true) do %>
<% end %>
in my application.js, i have this
//= require jquery
//= require jquery_ujs
//= require rails-ujs
//= require jquery3
//= require turbolinks
//= require cocoon
//= require bootstrap
//= require_tree .
Both somehow whenever this form is submitted, i get it as this
Started GET "/studios?utf8=%E2%9C%93&workout_category_id_list=&neighbourhood_list=Kuala+Lumpur&studio_id_list=" for 127.0.0.1 at 2018-12-28 12:42:12 +0800
Processing by StudiosController#index as HTML
It is processing as HTML instead of processing as JSON since remote: true is already added.
Any ideas as to why this happening. Running out of options
As per the description mentioned in the post it seems like you are concerned with the expected request type("json") not being sent over the controller.
So, for it work as mentioned in the post, change the code as below:
<%= form_tag(studios_path, method: :get, id: "studios_filter", remote: true, :html => {:'data-type' => 'json'}) do %>
This will be processing the request as json type.
since you are using method: :get in your form, all the form values get appended to the studios_path, you must use this method aspost and change the method in routes to post. After that you will start getting all the form fields in your controller as a hash
I struggled with the same issue: Rails 5 was processing my remote: true form submits as HTML no matter what I did. The solution for me was to add
//= require jquery_ujs
to application.js. OP already has this in their file above, which is what made me give it a try. Not sure why OP is still having issues, but this solved it for me ... so maybe this'll solve it for others!
When in responsive mode, bootstrap dropdown works on intial page visit/refresh but when another page is visited (via the dropdown link or any page link) it drops down but doesn't go back up. This happens when the <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> is placed at the bottom of the body tag. Everything works fine when the javascript tag is moved to the head but this is not best practice as regards js files and DOM load. My bootstrap files are added manually and not with a gem. Why is this happening this way? Is there something I am missing?
Here is my application.js file arrangement
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require bootstrap.min
//= require turbolinks
It's because of turbolinks.
Try "data-no-turbolink"
%ul.dropdown-menu{:role => 'menu', :'aria-labeledby' => "something", "data-no-turbolink" => true}
I am getting a ExecJS::RuntimeError when I try to view localhost:3000. It displays this:
(in C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/turbolinks-1.1.1/lib/assets/javascripts/turbolinks.js.coffee)
and highlights this as the main problem.
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
Without the line included the application works (user signup, user login) but the logout function doesn't work and I believe i need the above line included to reference the files in /app/assets/javascripts
Without the javascripts link the application works but any attempt to signout produces this error:
No route matches [GET] "/signout"
even though I have the following in my /config/routes.rb file:
match '/signout', to: 'sessions#destroy', via: 'delete'
/app/assets/javasacripts/application.js
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
I managed to find a way to fix it by removing the line //= require_tree . After looking into the error a little more I found another (old) SO topic that touched on the same subject but it didn't give a full reason this solution worked; only briefly mentioned that, while allowing the application to work, this was ignoring the problem rather than fixing it.
I am a newbie in Ruby on Rails and need to know what things we have to be careful about if we are willing to use jQuery in a Ruby on Rails application.
In my view page I have:
<script type="text/javascript" src= "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<%= javascript_include_tag "application" %>
<script>
alert("hussain")
alert($('#hussi').val())
</script>
It gives the first alert as expected, but for the second alert it says '$' is not defined.
I have the jquery-rails gem installed.
The browser points out that I have a missing reference in my application.js file.
require jquery;
require jquery_ujs;
I saw some file examples where they mention it like:
= require jquery;
= require jquery_ujs;
But adding'=' raises an IDE error in my IDE.
Add this to you application.js:
//= require jquery
//= require jquery_ujs
and add this to your Gemfile:
gem 'jquery-rails'
Also make sure you have this in you application layout:
<%= javascript_include_tag "application" %>
Just a note: avoid using tags in your code, move your javascript to your assets folder.
There is a typo in your code: text/javas c ript. This causes the script to not be parsed.
When I press the link, the confirmation pops up, but no matter if I answer Yes or No, it will always execute the action and delete the record.
View:
link_to "Remove", gamep, :method => :delete, :confirm => "Remove "+#player.name+" from "+gamep.game.title+"?"
Action:
def destroy
#gameplayer = GamePlayer.find(params[:id])
#gameplayer.destroy
respond_to do |format|
format.html { redirect_to #gameplayer.player }
end
end
Yes I have
javascript_include_tag :defaults
Edit:
I had to fix this originaly using a jquery.rails.js because the :delete method wasn't working. Without this, there is no confirmation, and the link_to just acts like a normal link.
For some reason, when I installed the 'jquery-rails' gem, it didnt put a 'jquery-ujs.js' in my 'public/javascripts' folder.
C:/Ruby192/lib/ruby/gems/1.9.1/gems/jquery-rails-1.0.16
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-3.1.1
application.js
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//= require jquery
//= require jquery_ujs
//= require_tree .
In Rails 3.1, you should be using "application", not "defaults", otherwise none of your javascript will get loaded properly:
javascript_include_tag "application"
Also, you don't need to manually include any additional javascript files, as the application manifest (app/assets/javascripts/application.js) will already pull them all in. Just make sur e*all* of your JS files are sitting in app/assets/javascripts, not 'public/javascripts`.
And make sure you are not using the rails.js file from previous versions of Rails. This has been replaced by the jquery-rails gem in Rails 3.1.