Hi all, looking into ajax support on rails but I can't seem to get
sometime simple to work.

I basically started off with a simple scaffold app, seeded the
database with some dummy data and trying to get delete working as a
ajax request.

rails g scaffold pony name:string profession:string


My delete like looks like this (added remote: true).

<%= link_to 'Destroy', pony, method: :delete, remote: true, class:
'delete_pony' %>


I created a new file: app/views/ponies/destroy.js.erb which I don't
see getting called as I added an alert alert box to check.

$('delete_pony').ajaxSuccess( function() {
    alert('deleting...');
    $(this).closest('tr').fadeOut();
});

My controller method looks like this

   def destroy
     @pony.destroy
     respond_to do |format|
       format.html { redirect_to ponies_url, notice: 'Pony was
successfully destroyed.' }
       format.json { head :no_content }
       format.js { render layout: false }
     end
   end

What am I missing?

--
Kind Regards,
Rajinder Yadav

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAMRGebgy4JowV7d%3Dk-0akbhONaTeYXOM7w%3DPDVysf%3DGRHh8j5g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Lazarus Lazaridis at Apr 9, 2016 at 1:22 am
    Hi,

    If you take a look at your server's console you will see the error. From
    what I see, the second argument of the link helper expects a url but you
    pass an object (I suppose this variable exists otherwise the view with the
    link wouldn't render).

    This should work:

    <%= link_to 'Destroy', pony_path(pony), method: :delete, remote: true, class
    : 'delete_pony' %>

    Thanks,

    --
    Lazarus Lazaridis
    www.arubystory.com <http://www.arubystory.com>| https://github.com/iridakos |
    @lazaru_s <https://twitter.com/lazaru_s>
    On Saturday, April 9, 2016 at 4:04:24 AM UTC+3, Rajinder Yadav wrote:

    Hi all, looking into ajax support on rails but I can't seem to get
    sometime simple to work.

    I basically started off with a simple scaffold app, seeded the
    database with some dummy data and trying to get delete working as a
    ajax request.

    rails g scaffold pony name:string profession:string


    My delete like looks like this (added remote: true).

    <%= link_to 'Destroy', pony, method: :delete, remote: true, class:
    'delete_pony' %>


    I created a new file: app/views/ponies/destroy.js.erb which I don't
    see getting called as I added an alert alert box to check.

    $('delete_pony').ajaxSuccess( function() {
    alert('deleting...');
    $(this).closest('tr').fadeOut();
    });

    My controller method looks like this

    def destroy
    @pony.destroy
    respond_to do |format|
    format.html { redirect_to ponies_url, notice: 'Pony was
    successfully destroyed.' }
    format.json { head :no_content }
    format.js { render layout: false }
    end
    end

    What am I missing?

    --
    Kind Regards,
    Rajinder Yadav
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/12928fcc-67f4-4f66-a54c-c53e55c69116%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rajinder Yadav at Apr 9, 2016 at 1:25 am
    OK I just noticed inside my jquery I was missing the dot '.' before
    the class name!
    On Fri, Apr 8, 2016 at 9:03 PM, Rajinder Yadav wrote:
    Hi all, looking into ajax support on rails but I can't seem to get
    sometime simple to work.

    I basically started off with a simple scaffold app, seeded the
    database with some dummy data and trying to get delete working as a
    ajax request.

    rails g scaffold pony name:string profession:string


    My delete like looks like this (added remote: true).

    <%= link_to 'Destroy', pony, method: :delete, remote: true, class:
    'delete_pony' %>


    I created a new file: app/views/ponies/destroy.js.erb which I don't
    see getting called as I added an alert alert box to check.

    $('delete_pony').ajaxSuccess( function() {
    alert('deleting...');
    $(this).closest('tr').fadeOut();
    });

    My controller method looks like this

    def destroy
    @pony.destroy
    respond_to do |format|
    format.html { redirect_to ponies_url, notice: 'Pony was
    successfully destroyed.' }
    format.json { head :no_content }
    format.js { render layout: false }
    end
    end

    What am I missing?

    --
    Kind Regards,
    Rajinder Yadav


    --
    Kind Regards,
    Rajinder Yadav

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAMRGebiwz9bGW-BJx4WoR4GYQ0DAvkko2FDK3B7_Vz_zM_cccg%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rajinder Yadav at Apr 9, 2016 at 4:26 pm
    Hi Lazaruz,

    in the link_to call, when you only provide a variable, by rails
    conventions the object's id value will be used and the show method
    will be called.

    fyi there were actually two correction I had to make in the javascript
    code, in addition to what I mentioned earlier, I also has to change
    jQuery method "ajaxSuccess" to use the "on" method like this.

    $('.delete_pony').on( 'ajax:success', function() {
    On Fri, Apr 8, 2016 at 9:25 PM, Rajinder Yadav wrote:
    OK I just noticed inside my jquery I was missing the dot '.' before
    the class name!
    On Fri, Apr 8, 2016 at 9:03 PM, Rajinder Yadav wrote:
    Hi all, looking into ajax support on rails but I can't seem to get
    sometime simple to work.

    I basically started off with a simple scaffold app, seeded the
    database with some dummy data and trying to get delete working as a
    ajax request.

    rails g scaffold pony name:string profession:string


    My delete like looks like this (added remote: true).

    <%= link_to 'Destroy', pony, method: :delete, remote: true, class:
    'delete_pony' %>


    I created a new file: app/views/ponies/destroy.js.erb which I don't
    see getting called as I added an alert alert box to check.

    $('delete_pony').ajaxSuccess( function() {
    alert('deleting...');
    $(this).closest('tr').fadeOut();
    });

    My controller method looks like this

    def destroy
    @pony.destroy
    respond_to do |format|
    format.html { redirect_to ponies_url, notice: 'Pony was
    successfully destroyed.' }
    format.json { head :no_content }
    format.js { render layout: false }
    end
    end

    What am I missing?

    --
    Kind Regards,
    Rajinder Yadav


    --
    Kind Regards,
    Rajinder Yadav


    --
    Kind Regards,
    Rajinder Yadav

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAMRGebicF_JkT5wzctSqf4sx4GubfEo%3D%3D3NcB6nj95UxRSS04A%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 9, '16 at 1:03a
activeApr 9, '16 at 4:26p
posts4
users2
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase