2013-10-02 23 views
0

有一個簡單的資產顯示頁面,它具有部分「_assign_group」中的「建議組」鏈接。 當我點擊這個遠程鏈接正確的行動被調用,但卡住了錯誤。如何使用鏈接到軌道中的遠程工作在ajax 3.2

_assign_group.html.erb

<% if @asset.has_suggested_group(@group)%> 
    Already suggested to <%= @group.name %> 
<% else %> 
    <%= link_to "Suggest to #{@group.name}", add_asset_group_path(:asset_id => @asset.id, :group_id => @group.id), :remote => true %> 
<% end %> 

asset_controller.rb

class AssetsController < ApplicationController 

    def show 
     @asset = Asset.find_by_id(params[:id]) 
     @group = Group.find_by_id(params[:group_id]) 
     respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @asset } 
     end 
    end 



    def add_group 
     @asset_group = AssetGroup.new(params[:asset_group]) 
     respond_to do |format| 
      if @asset_group.save 
    #  format.html { redirect_to asset_path(params[:asset_group][:asset_id]), notice: 'Asset added to this group.' } 
      format.js { render add_group.js } 
      else 
    #  format.html { redirect_to asset_path(params[:asset_group][:asset_id]), notice: "Asset is already added to #{@asset_group.group.name}" } 
      format.js { render add_group.js } 
      end 
     end 
     end 
    end 

add_group.js.erb

$("#assign_group_to_asset").html("<%= escape_javascript(render(:partial => "assign_group")) %>"); 

的routes.rb

resources assets 
resources groups 
post 'assets/add_group' => "assets#add_group", :as => :add_asset_group 

是error.log

Started GET "/assets/add_group?asset_id=4&group_id=160" for 127.0.0.1 at 2013-10-01 21:11:55 +0530 
Processing by AssetsController#show as JS 
    Parameters: {"asset_id"=>"4", "group_id"=>"160", "id"=>"add_group"} 
    User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 28 LIMIT 1 
    (0.2ms) BEGIN 
    (0.6ms) UPDATE "users" SET "last_request_at" = '2013-10-01 15:41:55.423956', "perishable_token" = 'Jjs9ITpltxKXrwLxShG', "updated_at" = '2013-10-01 15:41:55.424931' WHERE "users"."id" = 28 
[paperclip] Saving attachments. 
    (250.2ms) COMMIT 
    Asset Load (1.5ms) SELECT "assets".* FROM "assets" WHERE "assets"."id" = 0 LIMIT 1 
    Group Load (1.3ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = 160 LIMIT 1 
    Rendered assets/show.html.erb within layouts/application (2.3ms) 
Completed 500 Internal Server Error in 278ms 

ActionView::Template::Error (undefined method `asset_image' for nil:NilClass): 
    15:   <div class="span2 dashboard_boxes_autoht"> 
    16:   <div class="image_border"> 
    17:    <div> 
    18:    <% if @asset.asset_image && @asset.asset_image.image.exists?%> 
    19:     <%= image_tag @asset.asset_image.image(:medium)%> 
    20:    <% else %> 
    21:     <%= image_tag "logo.png", :size => '100x100' %> 
    app/views/assets/show.html.erb:18:in `_app_views_show_html_erb__300220498_88250470' 
    app/controllers/assets_controller.rb:31:in `show'`enter code here` 

在日誌 「show.html.erb」 得到呈現,而不是局部的 「assign_group」。有什麼建議麼?

+0

看起來像你的路由問題。你能發佈你的'routes.rb'內容嗎? –

+0

只是添加了路線代碼 – chaitanya

回答

1

嘗試在你的routes.rb

resources assets do 
    post :add_group, :on => :member 
end 

下,改變你的形式使用:

add_group_assets_path(@asset.id, :group_id => @group.id)