Delete method in ROR route error when using remote true - ruby-on-rails

I'm trying to delete post with the remote: true command in rails. Everything is working fine when i'm not using ajax. But when i use the remote true command i get an routing error.
view:
<% #posts.each do |post| %>
<%= post.title %>
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
<% end %>
controller:
def index
#posts = Post.all
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to :back
end
routes:
resources :posts
This is the error i get in the log.
Started DELETE "/posts" for 127.0.0.1 at 2014-10-11 12:46:33 +0200
ActionController::RoutingError (No route matches [DELETE] "/posts"):
Thanks in advance.
Update. I get this when i write rake routes.
DELETE /posts/:id(.:format) posts#destroy

To get this to working.
Change:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
redirect_to :back
end
To this:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.js { head :no_content }
end
end
and:
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
to:
<%= link_to 'delete', post, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>

Incase anyone is having any problems with this, I managed to solve it by adding :data => { :type => :json } to the link.
For example:
<%= link_to "Delete this post", #post, :method => :delete, :remote => true, :data => { :type => :json } %>
I imagine UJS is expecting HTML, however JSON is returned so it fails and posts again (I noticed two requests).
Hope this helps!

Related

Rails acts_as_votable using AJAX to stop page reloads

I'm trying to be able to upvote using acts_as_votable without the page reloading but I'm not sure how to change the routes to make it work
Controller
def upvote
#video = Video.find(params[:id])
#ip = request.remote_ip
# Ipaddresstracker.delete_all
if Ipaddresstracker.find_by(ipaddress: #ip)
else
Ipaddresstracker.create(:ipaddress => #ip, :upvoted => true, :upvotedcount => 1)
#video.vote_by voter: User.first, :duplicate => true
end
# redirect_to :back
respond_to do |format|
format.html { redirect_to :back }
format.js { render layout: false }
end
end
View
<%= link_to like_video_path(x), method: :get, remote: true, class: "btn btn-default btn-sm" do %>
Route
resources :videos do
member do
put "like", to: "videos#upvote"
put "dislike", to: "videos#downvote"
end
end
Error
ActionController::RoutingError (No route matches [GET] "/videos/53/like"):
The route expects a put request. Change the :method option to :put
<%= link_to like_video_path(x), method: :put, remote: true, class: "btn btn-default btn-sm" do %>
Actually, if you had Jquery you can solve this problem by Jquery.
If not, try to change this format.js { render layout: false }. You need to render nothing so you can set render :nothing => true in your format.js block

Routing Error No route matches [DELETE] "/topics"

Trying to get delete action for topics controller to work properly.
Here is my topics controller
def destroy
#topic = Topic.find(params[:id])
if #topic.destroy
flash[:notice] = "\"#{#topic.title}\" Topic was deleted!"
redirect_to topics_path(#topic)
else
flash.now[:alert] = "An error occurred. Please try again."
render :show
end
end
My routes:
resources :topics do
resources :bookmarks, except: [:index]
end
My delete link on my index view:
<%= link_to "Delete Topic", #topic, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>
Really don't understand what I am missing.
Try with these changes:
def destroy
#topic = Topic.find(params[:id])
if #topic.destroy
flash[:notice] = "\"#{#topic.title}\" Topic was deleted!"
redirect_to topics_path
else
flash.now[:alert] = "An error occurred. Please try again."
render :show
end
end
Delete Link:
<%= link_to "Delete Topic", topic_path(#topic), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>

Routing error with confirmed (seemingly) existing route

I'm copying what seems to be identical logic, but it doesn't work with one of my models.
In surveys, I have
View
<% #surveys.each do |survey| %>
...
<%= link_to 'Delete', survey, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
Controller
def destroy
#survey = Survey.find(params[:id])
#survey.destroy
respond_to do |format|
format.html { redirect to '/' }
format.json { head :no_content }
end
end
The delete function works fine.
Yet in question, I have
View
<% #questions.each do |question| %>
...
<%= link_to 'Delete', question, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
Controller
def destroy
#survey = Survey.find(params[:survey_id])
#question = Question.find(params[:id])
#question.destroy
respond_to do |format|
format.html { redirect to #survey }
format.json { head :no_content }
end
end
This gives me the error:
undefined method `question path' for #<#<Class:0x008ff2534....
When I remove the link_to, it works just fine retrieving question and its properties.
Changing the logic in my view to something more specific,
<%= link_to "Delete", :controller => "questions", :action => "destroy", :id => question.id %>
I get a more specific error.
No route matches {:controller=>"questions", :action=>"destroy", :id=>1}
Running rake routes, this confirms the path exists.
DELETE /surveys/:survey_id/questions/:id(.:format) questions#destroy
And here's my routes.rb entry:
devise_for :users do
resources :surveys do
resources :questions do
resources :responses
end
end
end
Computers don't make mistakes, so what did I do wrong?
questions are nested resources, so you should also pass survey to the path:
<%= link_to 'Delete', [#survey, question], :confirm => 'Are you sure?', :method => :delete %>
Assuming that you have set #survey variable.
Question is a nested resource under Survey so your route needs to reflect that. Note that in the rake routes output there's a :survey_id parameter as part of the route. It is required. Therefore your link needs to look like this:
<%= link_to "Delete", :controller => "questions", :action => "destroy", :survey_id => #survey.id, :id => question.id %>
Alternatively you can use Marek's path, namespacing the question resource:
<%= link_to 'Delete', [#survey, question], :confirm => 'Are you sure?', :method => :delete %>

Rails dynamic increasing button

I have to do an increasing value button "bg" for p1 entry in table game but it is not working... It save the game with p1 = 0 but the link_to has no effect..
Many thanks for any help!!! (And sorry for my bad English...)
controller
def create
#game = Game.new(params[:game])
#game.p1 = 0
respond_to do |format|
if #game.save
format.html { render action: "show" }
end
end
end
def show
#game = Game.find(params[:id])
respond_to do |format|
format.js {render 'hnew'}
end
end
def add_three_points
#game = Game.find(params[:id])
#game.update_attribute(:p1, #game.p1 + 3)
end
views
show.html.erb
<%= render 'hnew' %>
hnew.js.erb
$('#ajax_hidden').html("<%= escape_javascript(render('hnew')) %>");
_hnew.html.erb
<%= link_to "bg", add_three_points_path(#game.id), {remote: true, method: :put},
class: 'btn btn-small' %>
<%= #game.p1 %>
error message:
Started PUT "/add_three_points.14" for 127.0.0.1 at 2013-07-10 21:27:01 +0200
Processing by GamesController#add_three_points as
Completed 404 Not Found in 1ms
ActiveRecord::RecordNotFound (Couldn't find Game without an ID):
app/controllers/games_controller.rb:86:in `add_three_points'
The :method option of link_to is for the HTTP verb (:get, :post, :put or :delete)
What you want to do is below (using the :put method, preferred verb for updates)
<%= link_to "bg", add_three_points_path(#game), {remote: true, method: :put}, class: 'btn btn-small' %>
and in routes.rb:
put 'add_three_points' => 'games#add_three_points', :as => :add_three_points

undefined method on Passing Id for reference table

I have two tables: client and client info. I am displaying client info on the client show page. Now I am trying to delete a specific client info in my client show page. I have:
<%= link_to "Delete", [ #client.id, #client.client_info ],
:confirm => 'Are you sure?',
:method => :delete,
:class => "btn"
%>
In my controller I am doing:
def destroy
#contact_info = ClientInfo.find(params[:id])
#contact_info.destroy
respond_to do |format|
format.html { redirect_to #client }
format.json { head :ok }
end
I am getting an error saying undefined method.

Resources