I am trying to submit a simplel form in Rails 3, to use with the acts_as_commentable plugin.
#new_comment_box
#comment_input_box
= form_for :comment, :url => comments_path(commentable.class.to_s.underscore, commentable.id), :html => {:id => "new_comment"}, :remote => true do |f|
#share-input
= f.text_area :comment, :id => 'comment-input', :'data-text' => "Comment on this review...", :placeholder => "Comment on this review..."
.cancel
.clear
= f.submit 'Comment', :id => 'item-comment-submit', :class => "button"
.clear
My Comment model has a "comment" text attribute, and My controller is pretty simple:
class CommentsController < ApplicationController
before_filter :load_commentable
def create
pp params[:comment]
@comment = @commentable.comments.build(params[:comment])
@comment.user = current_user
respond_to do |format|
if @comment.save
format.html {render :partial => 'comments/comment', :locals => {:commentable => @commentable, :comment => @comment}}
end
end
end
def destroy
@comment = @commentable.comments.find(params[:id])
@comment.destroy
respond_to do |format|
format.json {rend开发者_C百科er :json => params[:id]}
end
end
protected
def load_commentable
@commentable = params[:commentable_type].camelize.constantize.find(params[:commentable_id])
end
end
Here is the generated HTML:
<form accept-charset="UTF-8" action="/item/252/comments" data-remote="true" id="new_comment" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="cXgXwhXdMMV38Ye/fIVVrke8pH7iZE/iFY+z4oO2Szs=" /></div>
<div id='share-input'>
<textarea cols="40" data-text="Comment on this review..." id="comment-input" name="comment[comment]" placeholder="Comment on this review..." rows="20"></textarea>
<div class='cancel'></div>
</div>
<div class='clear'></div>
<input class="button" id="item-comment-submit" name="commit" type="submit" value="Comment" />
<div class='clear'></div>
</form>
and my routes look like this:
POST /:commentable_type/:commentable_id/comments(.:format) {:action=>"create", :controller=>"comments"}
Yet when I try to submit this form, the rails log detects nothing being sumitted:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cXgddasdMMV38Ye/fIVVrkdfsdpH7iZE/iFYsdfsdfz4oO2Szs=", "comment"=>{"comment"=>""}, "commit"=>"Comment", "commentable_type"=>"item", "commentable_id"=>"252"}
Furthermore, it looks from firebug like the request isn't even sending the comments from the page.
Could someone tell me what I'm doing wrong, and why this isn't working?
Thanks
For some reason.. it didn't like having the textarea id as "comment-input". When I changed it to "comment_comment" it worked... would still appreciate an explanation why though
So you figured out yourself why this was failing, which is good.
You shouldn't need to give this a custom id
attribute as it will already have one called comment_comment
based on the name of the field. Best not to mess with that.
Unfortunately, I do not know the answer as to why it wasn't accepting this weirdly named parameter. The code probably lies within the rails.js
or jquery_ujs.js
file in your application.
精彩评论