link_to confirm, no matter the answer will always execute the action - ruby-on-rails

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.

Related

rails 5 routing, PUT action winds up as GET

using acts_as_votable gem to upvote/downvote a "skill",
rails 5.0.0 app
routes.rb:
resources :skills do
member do
patch "upvote", to: "skills#upvote"
put "downvote", to: "skills#downvote"
end
end
skills controller:
def upvote
#skill.upvote_from current_user
redirect_to skill_path(#skill.id)
end
def downvote
#skill.downvote_from current_user
redirect_to skill_path(#skill.id)
end
the view:
<%= link_to "Up Vote", upvote_skill_path(skill), method: :put %>
generates this HTML:
<a rel="nofollow" data-method="put" href="/skills/98/upvote">Up Vote</a>
but when clicking the link, somehow its a GET:
Started GET "/skills/98/upvote" for 192.168.0.2 at 2017-02-23 22:46:07 -0500
ActionController::RoutingError (No route matches [GET] "/skills/98/upvote"):
'rake routes' output looks right:
upvote_skill PUT /skills/:id/upvote(.:format) skills#upvote
downvote_skill PUT /skills/:id/downvote(.:format) skills#downvote
Im at a loss.. the only thing I can think is this app started life out as an API only app http://edgeguides.rubyonrails.org/api_app.html but I am pretty sure I have followed all the convert "regular app to API" steps by doing section 3.2 backwards. But maybe I am missing something ?
Rails uses unobtrusive javascript to perform this type of actions, probably you don't have included the javascript correctly, because your app started as api-only and assets and views are not included in this mode.
In your application.html.erb ensure you are including the javascript
<!-- app/views/layouts/application.html.erb -->
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
You application.js must have at least these definitions
// app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
finally, the Gemfile needs
gem 'jquery-rails'
Then: bundle, reload the server and test again.

Rails 5 data-method=delete sends GET request

I'm having a problem setting my http verb to delete. This is my first app in rails 5 and I'm also using a purchased theme so I am sure I'm missing something stupid. When I create the link below:
<%= link_to 'Sign out', destroy_user_session_path, :method => :delete %>
I get
<a rel="nofollow" data-method="delete" href="/users/sign_out">Sign out</a>
however this is still sending a GET request. Most of the answers I've seen say to check if jquery ujs is getting loaded. It looks like it is. Am I missing anything else?
<script src="/assets/jquery_ujs.self-e87806d0cf4489aeb1bb7288016024e8de67fd18db693fe026fe3907581e53cd.js?body=1" data-turbolinks-track="reload"></script>
i have same problem.
what i did:
run delete all compiled assets
rails assets:clobber
change config/production.rb
config.assets.compile = true
application.js:
//= require jquery2
//= require jquery_ujs
hope it helps

ExecJS RuntimeError on Session Logout

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.

ruby rails assets path for 3rd party

I'm attempting to port from Rails 2.3.8 to Rails 3.2.6. My app currently uses 3rd party css/js/images which are placed in public/<3rdparty>/<subdir>/../..
In layout/application.html.erb, I used absolute path to retrieve them; eg <%= stylesheet_link_tag "/dojoroot/dojo/resources/dojo.css" %> and <%= javascript_include_tag "/dojoroot/dojo/dojo.js" %>
For Rails 3.2.6, the assets are expected to be in app/assets or lib/assets. However, moving the 3rd party from public to app/assets doesn't work because the tag are looking for the assets under their respective type. For example, <%= stylesheet_link_tag "dojoroot/dojo/resources/dojo.css" %> will look at app/assets/stylesheet/dojoroot/dojo/resources/dojo.csc and likewise the javascripts will look under apps/assets/javascripts/dojoroot/dojo/dojo.js. Breaking the 3rd party into 3 subdirs of stylesheets/javascripts/images seem rather wasteful and timeconsuming.
If I pass the absolute path to the tag; eg <%= javascript_include_tag "/dojoroot/dojo/dojo.js" %> Rails won't find it in app/assets/dojoroot.
Is there a configuration where I can have Rails look for the assets without injecting "stylesheets", "javascripts" into the path?
Just name the file, no path
<%= stylesheet_link_tag "dojo" %>
If you want to reference more than one, you can do
<%= stylesheet_link_tag "dojo", "custom" %>
Also, make sure in app/assets/javascript/application.js that you have
//= require jquery
//= require jquery_ujs
//= require_tree .
And <%= javascript_include_tag(:application) %> in your application.html.erb
read '2.1.2 Using index files' in http://edgeguides.rubyonrails.org/asset_pipeline.html

Rails: link_to with :remote => true not working

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!

Resources