Skip to content

Instantly share code, notes, and snippets.

Created January 11, 2011 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/774510 to your computer and use it in GitHub Desktop.
Save anonymous/774510 to your computer and use it in GitHub Desktop.
Problem explained
<% 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 %>
class Article < ActiveRecord::Base
attr_accessible :title, :subtitle, :content, :user_id, :date, :publish, :allowcomments
has_many :comments, :as => :item
end
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
class Comment < ActiveRecord::Base
belongs_to :item, :polymorphic => true
end
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
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
/**
* 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 );
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
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)
<% 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 } %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment