奇妙な問題があります。私は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