为什么Rails忽略了我提交此表单的参数?

时间:2011-08-01 22:51:01

标签: ruby-on-rails ruby-on-rails-3 forms

我正在尝试在Rails 3中提交一个简单的表单,以与acts_as_commentable插件一起使用。

#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

我的评论模型有一个“评论”文字属性,和 我的控制器非常简单:

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 {render :json => params[:id]}
    end 
  end

protected

  def load_commentable
    @commentable = params[:commentable_type].camelize.constantize.find(params[:commentable_id])
  end
end

以下是生成的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="&#x2713;" /><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> 

我的路线看起来像这样:

POST   /:commentable_type/:commentable_id/comments(.:format)          {:action=>"create", :controller=>"comments"}

然而,当我尝试提交此表单时,rails日志检测到没有任何结果:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"cXgddasdMMV38Ye/fIVVrkdfsdpH7iZE/iFYsdfsdfz4oO2Szs=", "comment"=>{"comment"=>""}, "commit"=>"Comment", "commentable_type"=>"item", "commentable_id"=>"252"}

此外,它从firebug看起来就像请求甚至没有从页面发送评论。

有人能告诉我我做错了什么,以及为什么这不起作用?

由于

2 个答案:

答案 0 :(得分:1)

出于某种原因......它不喜欢将textarea id作为“comment-input”。当我将其更改为“comment_comment”时,它仍然有效......仍然会理解为什么会这样解释

答案 1 :(得分:1)

所以你弄明白自己为什么失败了,这很好。

您不应该为此提供自定义id属性,因为根据字段名称,它已经有一个名为comment_comment的属性。最好不要惹这个。

不幸的是,我不知道答案为什么它不接受这个奇怪命名的参数。代码可能位于应用程序的rails.jsjquery_ujs.js文件中。