0

奇妙な問題があります。私は2つのモデルの問題、コメントを持っています。コメントはIssues内にネストされているため、コメントコントローラーで次のように作成アクションを実行します。

  def create
    @issue = Issue.find(params[:issue_id])
    @comment = @issue.comments.create!(params[:comment])

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
        format.js #create.js.erb
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

そして私のcreate.js.erb:

var new_comment = $("<%= escape_javascript(render(:partial => @comment))%>").hide();
$('#comments').prepend(new_comment);
$('#comment_<%= @comment.id %>').fadeIn('slow');
$('#new_comment')[0].reset();

Issue.rb

class Issue < ActiveRecord::Base
  attr_accessible :category, :description, :title

  has_many :comments
end

Comment.rb

class Comment < ActiveRecord::Base
  attr_accessible :body, :issue_id
  belongs_to :issue
end

ルート.rb

  resources :comments


  resources :issues do
    resources :comments
  end

問題: views / issues/show.html.erbに部分的に存在するフォームであるコメントを作成すると。コメントはデータベースに4回作成されます。

I couldn't locate what the problem was and whats causing it. Please help

4

2 に答える 2

0

私は実際に、js ファイルが /public/assets 内に配置された古い Rails バージョンに取り組んでいましたが、それがその奇妙な動作の理由でした。/public/assets フォルダー内のすべてのファイルを削除したところ、アプリは正常に動作するようになりました。

于 2012-11-24T08:07:37.770 に答える
0

まず、関連するコメントを作成します。

@comment = @issue.comments.build(params[:comment])

そして、コメントインスタンスを保存します

@comment.save

また、Javascript も確認してください。イベントのバブリングに問題があり、イベントが 2 回トリガーされている可能性があります。

于 2012-11-22T09:32:31.850 に答える