0

有一个简单的资产展示页面,在部分“_assign_group”中有“建议分组”链接。当我单击此远程链接时,正确的操作被调用但卡在错误中。

_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 %>

资产控制器.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")) %>");

路线.rb

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

错误日志

    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”。有什么建议么?

4

1 回答 1

1

在你的尝试以下routes.rb

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

并更改您的表格以使用:

add_group_assets_path(@asset.id, :group_id => @group.id)
于 2013-10-02T09:15:34.317 回答