i’m having a bit of trouble with adding a certain feature. i’m working on a buy/sell site and i want to be able to compare posts. here’s what i have so far:
in the posts view:
<%= button_to "Add to Compare", :action => "addCompare" %>
in the corresponding controller:
@@a = Array.new()
def addCompare
@@a << Post.id
end
so, all i want to do is add the post’s id to the array @@a. when i test this, i click on the “Add to Compare” button and I’m welcomed with this:
Template is missing
Missing template posts/addCompare with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rxml, :rjs, :builder, :rhtml, :erb]} in view paths “/home/mja32/470repo/traders/app/views”, “/var/lib/gems/1.8/gems/devise-1.4.2/app/views”
So I guess it’s trying to redirect to a view. How do I prevent it from doing this? All I want this button to do is to add the post’s id to the array and nothing more.
Thanks in advance,
Matt
First of all, storing persistent data in a controller’s class variable isn’t going to work the way you want it to. There’s no guarantee that
@@awill be the same array on your nextaddComparecall; for example, your nextaddComparecall could be handled by a different process. Also, what happens if two different clients calladdCompare? Do you really want to mix their data together in one pile? Probably not. Your first task is to replace@@awith a real per-user persistent store.If you want to return nothing at all from your controller, just do this at the end of your controller method:
That will tell Rails that something has already been rendered so it doesn’t need to try the default rendering action (which is to render the
posts/addCompareview) and returns nothing more than a 200 status code to the client.Once that’s in place, you’ll probably want to AJAXify your button with
:remote => true:So this:
Note that
button_tolooks like this:and that
:actionis foroptionsbut:remoteis forhtml_optionsso you have to explicitly set up the hashes with{}; you could just wrap theoptionsin braces:but I prefer the consistency of wrapping them both by hand.