0

I am using this tutorial , and have developed a simple application called ponies

this is what I have in view page ,

<% @ponies.each do |pony| %>
  <tr>
    <td><%= link_to 'Destroy', pony, method: :delete, data: { confirm: 'Are you sure?' }, :remote => true, :class => 'delete_pony' %> </td>
  </tr>
<% end %>

and in the controller :

def destroy
    @pony = Pony.find(params[:id])
    @pony.destroy

    respond_to do |format|
      format.html { redirect_to ponies_url }
      format.json { head :no_content }
      format.js   { render :layout => false }
    end
  end

clearly when i click on th link Destroy , script 'destroy.js.erb' file is executed

alert("in destroy.js.erb");

$('.delete_pony').bind('ajax:success', function() {  
    alert("in destroy.js.erb inside ajax success");
        $(this).closest('tr').fadeOut();
})

and succesfully deletes the pony object

but i create an extra link

<td><%= link_to 'hussi', pony , method: :post, data: { confirm: 'Are you sure?' }, :remote => true, :class => 'preview_pony' %></td>

created preview method in controller

def preview
    @pony = Pony.find(params[:id])
    @hussi = @pony.name
    puts @hussi

    respond_to do |format|
      format.html { redirect_to ponies_url }
      format.json { head :no_content }
      format.js   { render :layout => false }
    end
  end

and also created file 'preview.js.erb'

alert("in preview.js.erb");

$('.preview_pony').bind('ajax:success', function()
{
alert("you did it , hussain");
})

but on clicking th elink preview , script written inside 'preview.js.erb' is never called , any explanation why ??

and also in first link we use HTTP method :delete

now if i want simply to display some simeple method or to fade in/out some html element , what http method should i use

in the line :

<%= link_to 'hussi', pony , method: :post, data: { confirm: 'Are you sure?' }, :remote => true, :class => 'preview_pony' %>

what object should i pass in place of 'pony' what method should i use in place of ':delete' method

2 Answers 2

1

I think you have to define witch action to call in the controller:

<%= link_to 'hussi', {:action => "preview"}, method: :post, data: { confirm: 'Are you sure?' }, :remote => true, :class => 'preview_pony' %>

and remember to add the route to the action in the routes.rb file, like

post 'ponies/preview'

Also, as a side note, I recommend you check out the pry gem. You basically add

group :development do
  gem 'pry'
  gem 'pry-rails'
  gem 'pry-debugger'
  gem 'pry-stack_explorer'
end

To your Gemfile, run bundle install, (probably restart your server), and you can call binding.pry in your code as a break point.

Sign up to request clarification or add additional context in comments.

1 Comment

actually what i missed was tha in my prevew action i was taking the object passing a paramater id , but in case of preview link , there was no id being passed
0

If U are creating the preview link through ajax. then that might break. Try using the on('ajax:success',...

2 Comments

@AbibullahRahamathulah : my problem is , the new js file , 'preview.js.erb' is never called after clicking on the link , i have given alert in the very first line
hmmm. can you check the browser console to see if there are any javascript errors If u don't see any errors at your rails console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.