ruby-on-railsajaxlink-to-remote

how to work on ajax using link to remote in rails 3.2


Have a simple asset show page, which has link "suggest to group" in partial "_assign_group". When i click on this remote link right action get called but stuck in error.

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

In log "show.html.erb" got rendered instead of partial "assign_group". any suggestions?


Solution

  • Try the following in your routes.rb

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

    And change your form to use:

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