Reputation: 6617
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
Upvotes: 0
Views: 304
Reputation: 13736
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.
Upvotes: 1
Reputation: 2891
If U are creating the preview link through ajax. then that might break. Try using the on('ajax:success',...
Upvotes: 0