public
anonymous / _comments.html.erb
Last active

Problem explained

  • Download Gist
_comments.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13
<% if item.comments.empty? %>
No comments to display.
<% else %>
<% for comment in item.comments %>
<p><%= comment.created_at %> <%= link_to "Destroy", [item, Comment], :confirm => 'Are you sure?', :method => :destroy %></p>
<p><%= comment.body %></p>
<% end %>
<% end %>
Add Comment
<% form_for [item, Comment.new()] do |f| %>
<%= f.text_area :body %><br />
<%= f.submit %>
<% end %>
article.rb
Ruby
1 2 3 4
class Article < ActiveRecord::Base
attr_accessible :title, :subtitle, :content, :user_id, :date, :publish, :allowcomments
has_many :comments, :as => :item
end
articles_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class ArticlesController < ApplicationController
make_resourceful do
actions :all
response_for :create do
flash[:notice] = "Successfully created article."
redirect_to @article
end
response_for :update do
flash[:notice] = "Successfully updated article."
redirect_to @article
end
response_for :destroy do
flash[:notice] = "Successfully destroyed article."
redirect_to articles_url
end
end
end
comment.rb
Ruby
1 2 3
class Comment < ActiveRecord::Base
belongs_to :item, :polymorphic => true
end
comments_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
class CommentsController < ApplicationController
make_resourceful do
belongs_to :article, :newscast
actions :create, :destroy
 
response_for :create do
flash[:notice] = "Successfully created comment."
redirect_to parent_object
end
response_for :create_fails do
flash[:notice] = "Creating comment failed."
redirect_to parent_object
end
response_for :destroy do
flash[:notice] = "Successfully destroyed comment."
redirect_to parent_object
end
response_for :destroy_fails do
flash[:notice] = "Destroying comment failed."
redirect_to parent_object
end
 
#response_for :create, :create_fails, :destroy, :destroy_fails do
#redirect_to parent_object
#end
end
 
 
 
end
polymorphic nested comments make_resourceful routing problem, when deleting comments
1 2 3 4 5
if i visit for example http://0.0.0.0:3000/newscasts/3 and delete a comment there, i get routed to http://0.0.0.0:3000/articles/3/comments get the error:
 
No route matches "/articles/3/comments"
 
when i then go per typing url to http://0.0.0.0:3000/articles/3 the comment still exists
public/javascript/rails.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/**
* Unobtrusive scripting adapter for jQuery
*
* Requires jQuery 1.4.3 or later.
* https://github.com/rails/jquery-ujs
*/
 
(function($) {
// Triggers an event on an element and returns the event result
function fire(obj, name, data) {
var event = new $.Event(name);
obj.trigger(event, data);
return event.result !== false;
}
 
// Submits "remote" forms and links with ajax
function handleRemote(element) {
var method, url, data,
dataType = element.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
 
if (element.is('form')) {
method = element.attr('method') || 'POST';
url = element.attr('action');
data = element.serializeArray();
// memoized value from clicked submit button
var button = element.data('ujs:submit-button');
if (button) data.push(button);
} else {
method = element.attr('data-method') || 'GET';
url = element.attr('href');
data = null;
}
 
$.ajax({
url: url, type: method, data: data, dataType: dataType,
// stopping the "ajax:beforeSend" event will cancel the ajax request
beforeSend: function(xhr, settings) {
if (settings.dataType === undefined) {
xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
}
return fire(element, 'ajax:beforeSend', [xhr, settings]);
},
success: function(data, status, xhr) {
element.trigger('ajax:success', [data, status, xhr]);
},
complete: function(xhr, status) {
element.trigger('ajax:complete', [xhr, status]);
},
error: function(xhr, status, error) {
element.trigger('ajax:error', [xhr, status, error]);
}
});
}
 
// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
function handleMethod(link) {
var href = link.attr('href'),
method = link.attr('data-method'),
csrf_token = $('meta[name=csrf-token]').attr('content'),
csrf_param = $('meta[name=csrf-param]').attr('content'),
form = $('<form method="post" action="' + href + '"></form>'),
metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
 
if (csrf_param !== undefined && csrf_token !== undefined) {
metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
}
 
form.hide().append(metadata_input).appendTo('body');
form.submit();
}
 
function allowAction(element) {
var message = element.attr('data-confirm');
return !message || (fire(element, 'confirm') && confirm(message));
}
 
$('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
var link = $(this);
if (!allowAction(link)) return false;
 
if (link.attr('data-remote')) {
handleRemote(link);
return false;
} else if (link.attr('data-method')) {
handleMethod(link);
return false;
}
});
 
$('form').live('submit.rails', function(e) {
var form = $(this);
if (!allowAction(form)) return false;
 
if (form.attr('data-remote')) {
handleRemote(form);
return false;
}
});
 
$('form input[type=submit], form button[type=submit], form button:not([type])').live('click', function() {
var button = $(this);
if (!allowAction(button)) return false;
// register the pressed submit button
var name = button.attr('name'), data = name ? {name:name, value:button.val()} : null;
button.closest('form').data('ujs:submit-button', data);
});
 
/**
* disable-with handlers
*/
var disable_with_input_selector = 'input[data-disable-with]',
disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')',
disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';
 
var disable_with_input_function = function() {
$(this).find(disable_with_input_selector).each(function() {
var input = $(this);
input.data('enable-with', input.val())
.attr('value', input.attr('data-disable-with'))
.attr('disabled', 'disabled');
});
};
 
$(disable_with_form_remote_selector).live('ajax:before.rails', disable_with_input_function);
$(disable_with_form_not_remote_selector).live('submit.rails', disable_with_input_function);
 
$(disable_with_form_remote_selector).live('ajax:complete.rails', function() {
$(this).find(disable_with_input_selector).each(function() {
var input = $(this);
input.removeAttr('disabled').val(input.data('enable-with'));
});
});
})( jQuery );
routes.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11
Clangimmick::Application.routes.draw do |map|
map.resources :newscasts
map.resources :articles
 
map.resources :articles, :newscasts do |item|
item.resources :comments, :only => [:create, :destroy]
end
 
map.resources :flags
map.root :flags
end
server.log
1 2 3 4 5 6 7 8 9 10
Started GET "/articles/1" for 127.0.0.1 at 2011-01-11 16:12:48 +0100
Processing by ArticlesController#show as HTML
Parameters: {"id"=>"1"}
Article Load (0.2ms) SELECT "articles".* FROM "articles" WHERE ("articles"."id" = 1) LIMIT 1
SQL (0.2ms) SELECT COUNT(*) FROM "comments" WHERE ("comments".item_id = 1 AND "comments".item_type = 'Article')
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE ("comments".item_id = 1 AND "comments".item_type = 'Article')
DEPRECATION WARNING: <% %> style block helpers are deprecated. Please use <%= %>. (called from _app_views_shared__comments_html_erb__2272147928266429483_35823720_1467493481828575843 at /home/malice/my_websites/clangimmick/v0.0.5/clangimmick/app/views/shared/_comments.html.erb:10)
Rendered shared/_comments.html.erb (20.8ms)
Rendered articles/show.html.erb within layouts/application (64.8ms)
Completed 200 OK in 79ms (Views: 68.2ms | ActiveRecord: 0.7ms)
show.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<% title "Article" %>
 
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Subtitle:</strong>
<%= @article.subtitle %>
</p>
<p>
<strong>Content:</strong>
<%= @article.content %>
</p>
<p>
<strong>User:</strong>
<%= @article.user_id %>
</p>
<p>
<strong>Date:</strong>
<%= @article.date %>
</p>
<p>
<strong>Publish:</strong>
<%= @article.publish %>
</p>
<p>
<strong>Allowcomments:</strong>
<%= @article.allowcomments %>
</p>
 
<p>
<%= link_to "Edit", edit_article_path(@article) %> |
<%= link_to "Destroy", @article, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", articles_path %>
</p>
 
<%= render :partial => "shared/comments", :locals => { :item => @article } %>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.