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
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!
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.
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.
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.
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!