Rails JS响应创建操作失败

时间:2014-12-01 15:42:27

标签: ruby-on-rails ajax ruby-on-rails-4

在Rails 4中发布远程表单后,我无法重新加载当前页面,以便在我的数据库中创建连接对象。它适用于我的应用程序中的另一个模型关联,但不是这里....

查看表单:

从下拉列表中选择要分配的集合:

<% unless current_user == nil %>
<div class="btn-group pull-right">
  <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
    Collect
<span class="caret"></span>
  </a>
  <ul class="dropdown-menu">
     <% current_user.collections.each do |collection| %>
    <% unless  CollectionPiece.where(:collection_id => collection.id, :piece_id => @piece.id).exists? %>
    <li><%= link_to raw("<i class='glyphicon glyphicon-plus'></i>  #{collection.title}"), collection_pieces_path(:collection_piece => {:piece_id => @piece.id, :collection_id => collection.id}), :remote => true, class: "", :method => :post %></li>
    <% end %>
     <% end %>
        <li><%= link_to raw("<i class='glyphicon glyphicon-th-large'></i>  New Collection"), new_collection_path, :class => "" %></li>
   </ul>
 </div>
 <% end %>

Collection Piece控制器:

class CollectionPiecesController < ApplicationController


after_action :collection_email, only: :create


def create
    @collection_piece = CollectionPiece.create(collection_piece_params)

    respond_to do |format|
      if @collection_piece.save
        format.html { redirect_to :back, notice: 'Collection was successfully created.' }
      else
        format.html { redirect_to :back, notice: 'Collection was not successfully created.' }
      end
    end
end

def destroy
    @collection_piece = CollectionPiece.where(collection_piece_params)
    respond_to do |format|
      if @collection_piece.first.destroy
        format.js { redirect_to :back, notice: 'Collection was successfully destroyed.' }
      else
        format.js { redirect_to :back, notice: 'Collection was not successfully destroyed.' }
      end
    end
end

def collection_email
    @profile = current_user.profile
    @piece = Piece.find(params[:collection_piece][:piece_id])
    @collection = Collection.find(collection_piece_params[:collection_id])
    UserMailer.collection_email(@profile.user, @piece, @collection).deliver
     end

   private

# Never trust parameters from the scary internet, only allow the white list through.
def collection_piece_params
  params.require(:collection_piece).permit(:piece_id, :collection_id)
end



end

create.js.erb:

console.log("why you no work?");
location.reload();

希望您能提供帮助,如果您需要更多信息,请告诉我们?

0 个答案:

没有答案