Undefined method `destroy' for "Tech":String - ruby-on-rails

When I go to delete a post I keep receiving an error that says
undefined method `destroy' for "Tech":String
The "tech" part of the posts varies with whatever the tag is of the post I am trying to delete. I am not sure what the problem is. I am using acts_as_taggable_on if that has anything to do with it.
This is my destroy method in my posts controller:
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(root_path) }
format.xml { head :ok }
format.json { head :ok }
end
end
The delete button in my post show:
<%= button_to 'Delete', #post, :method => :delete, :confirm => "Are you sure?" %>
Tags are saved in the database as a string.

It's impossible to give you an answer to fix your problem, because you are giving no code and no examples.
Nonetheless that error means you are calling the destroy method on a string, and strings in Ruby do not have a destroy method defined.
Check where are you calling destroy, because it seems you are doing it in some function that returns a string (a tag name in your case). You must do it on an object that is an instance of the Tag class.

Related

Give an error when a model is deleted in Rails

Is there a way to give back an error if a particular model is deleted in Rails 3.x+? If it's deleted via the web, an error code is given back. If it's deleted via console then a message is given with the reason it can't be deleted.
You can use a before_destroy. The link is here. Returning false from this validation message prevents the destruction of the object.
Well, the way that I usually delete something is...
from the view:
<%= link_to "Delete Post", #post, method: :delete, data: { confirm: "You're sure you want to DELETE the post '#{#post.title}'?" } %>
then in the post controller:
def destroy
Post.find(params[:id]).destroy
flash[:success] = "Blog entry deleted!"
redirect_to posts_path
end
So, the method: :delete just gets to the destroy route of the controller. Then you do what you want, right?
So instead of .destroy after you find the model, you could do:
def destroy
#post = Post.find(params[:id])
flash[:notice] = "Blog entry can't be deleted..."
puts "#{#post.name} can't be deleted because..." #this line is what shows up in the console
redirect_to posts_path
end

Why is my dependent destroy not working in Rails?

Thanks for your help with this! I have many newsavedmaps, and each can have multiple waypoints. The waypoints table connects to the newsavedmaps id with a field, "newsavedmap_id." I am new to Rails (using Rails 2), and I'm having trouble with a destroy feature. Essentially, if a user gets rid of a newsavedmap, I also want to eliminate the waypoints.
newsavedmap.rb
has_many :waypoints, :dependent => :destroy
waypoint.rb
belongs_to :newsavedmap
newsavedmap controller (I think this is the problem)
def destroy
#newsavedmap = Newsavedmap.find(params[:id])
#waypoint = Waypoint.find(params[:id])
#newsavedmap.destroy
#waypoint.destroy
respond_to do |format|
format.html { redirect_to "/CODE" }
format.xml { head :ok }
end
end
I also have a view, newmaps.html.erb. A user sees his newsavedmap on the page, and he can click a link:
When he does, this is the javascript that kicks in:
$('.newremovemap').click(function(){
if (confirm('Are you sure you want to remove this map?')) {
var mappage = $(this).closest('.wholemap');
var map = $(this).parent();
$.post(this.href, { _method: "delete", authenticity_token: $('#auth_token').val() }, function(){
mappage.fadeOut();
});
};
return false;
})
Now, when I click the link, the alert message repeats many times, but the records are not removed. The error code is ActiveRecord::RecordNotFound (Couldn't find Waypoint without an ID). I've tried applying links like this to the problem (
Rails dependent destroy error), but I can't seem to find a solution.
you need to just destroy the newsavedmap, and associated waypoints will be automatically deleted.
def destroy
#newsavedmap = Newsavedmap.find(params[:id])
#newsavedmap.destroy
respond_to do |format|
format.html { redirect_to "/CODE" }
format.xml { head :ok }
end
end
the other most important thing is that with a single params[:id], you are trying to find objects of two classes(Newsavedmap,Waypoint), that is wrong. as per the code link is for Newsavedmap and hence you can find its object by params[:id], not of Waypoint.
One more thing, that is important that you are trying to call a javascript function on that click, which is redirecting to newsavedmap show page.Try to change that link as well :
<%= link_to 'Destroy', newsavedmap_path(#newsavedmap),
:confirm => 'Are you sure?', :method => :delete %>
If you have added the dependent destroy in newsavedmap you don't need to call
#waypoint.destroy // not needed

serving javascript with rails

I have this code down here. When format.js fires I want to serve to the client a javascript file. How do I do that?
class LineItemsController < ApplicationController
def destroy
#line_item = LineItem.find(params[:id])
#line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url }
format.js {}
format.json { head :no_content }
end
end
I have a file called destroy.js.erb in the controller, but that doesn't run automatically. I tried many combinations but nothing seems to work...
what do I put inside format.js { ??? } to serve a javascript file I want? I don't want to write vanilla javascript.
To trigger your js-response of your destroy action try:
<%= button_to 'Remove', #line_item, method: :delete, remote: :true %>
For testing purpose make your destroy.js.erb as the following:
alert("Line item with id <%= #line_item.id %> has been removed");
You can leave your format.js w/o bracket (or with empty ones).

How to delete a specific post from a model in rails?

Since, I am new to rails, so I have want to know a small functionality.
I have a reports model in my rails 3 application(not by scaffolding). I am displaying reports one by one through ajax functionality. I want to add a delete link to my each report. I have also created the destroy method in my controller. Now, I don't know how to delete a specific report when I click on the delete link of that particular report.
Here's my controller code:-
class ReportsController < ApplicationController
def index
#reports = Report.all(:order => "created_at DESC")
respond_to do |format|
format.html
end
end
def create
#report = Report.create(:description => params[:description])
respond_to do |format|
if #report.save
format.html { redirect_to reports_path }
format.js
else
flash[:notice] = "Report failed to save."
format.html { redirect_to reports_path }
end
end
end
def destroy
#report = Report.find(params[:id])
if #report.destroy
format.html { redirect_to reports_path }
format.js
end
end
end
You can assume that my reports are being displayed in the twitter-timeline format and I want to add the delete report feature to each report. Please help me out.
In your view you'd add a link, button, etc. to send the delete action back to the server.
Using link_to for example:
link_to("Destroy", report_path(report), :method => :delete, :confirm => "Are you sure?")
You can do the same with button_to.
Update:
Sorry I missed the AJAX mention (thanks Jeffrey W.).
You'll also want to add :remote => true if you want to send the delete via AJAX.

Rails 3, changing a specific value in a row

This question seems ridiculously easy, but I seem to be stuck.
Lets say we have a table "Books"
Each Book, has a name, description, and a status.
Lets say I want to create link in the show view, that when clicked, solely changes the status (to "read") for example.
So far, I've tried adding a block in the controller, that says:
def read
#book = Book.find(params[:id])
#book.status = "Read"
#book.update_attributes(params[:book])
respond_to do |format|
format.html { redirect_to :back}
format.xml { render :xml => #book }
end
end
Then I've added a link to the view that is like:
<%= link_to "Read", read_book_path(#book), :method => :put %>
This isn't working at all. I have added it to my routes, but it doesn't seem to matter.
Any help would be great! Thanks!
-Elliot
EDIT: Forgot to add I'm getting a NoMethodError: undefined method `read_book_path'
You need to define a method called read:
def read
#book = Book.find(params[:id])
#book.status = "Read"
#book.save
end
Then you want <%= link_to "Read", read_book_path(#book), :remote => true %>
and in routes.rb:
resources :books do
member do
get 'read'
end
end
Once that works, mess around with changing the method to :put
If all you're trying to do is set the status attribute on a book and not update any other parameters, then calling update_attributes is unnecessary.
change:
#book.update_attributes(params[:book])
to:
#book.save!

Resources