views:

46

answers:

1

Is it possible to use ruby code in RJS files?

For example, the destroy.js.rjs file

if @template == "viewer"
  page["viewing_registry_#{@viewer_registry.id}"].replace_html :partial => "shared/request_viewer_link" 
else
  page["viewer_#{@viewer.id}"].visual_effect :DropOut, :duration => 2.0
  flash.discard
end

This is the RJS file called from a destroy action that has a

  def destroy
    @viewer = Viewer.find(params[:id])
    @viewer_registry = Registry.find(@viewer.registry_id)
    @viewer.destroy
    flash[:notice] = "Viewer deleted"
    @template = params[:template]
    params[:template] = nil
    respond_to do |format|
      format.html { redirect_to registry_path(@viewer_registry) }
      format.js
    end
  end

So from an AJAX call, the RJS file is used for the response, and it gives a different response based on which template is calling the destroy action.

At the moment, the AJAX call is made, the record is destroyed, and then nothing happens, regardless of which template was calling for the destroy action. So I'm wondering whether it is not working simply because I can't use ruby code in an RJS file. Any ideas? Or am I doing it wrong entirely?

Thanks!

A: 

Right, I redid the destroy function so that it looked like this:

  def destroy
    #is this bad because its not linked to current_user?
    #how would you even set it to be relational to current_user? 
    @viewer = Viewer.find(params[:id])
    @viewer_registry = Registry.find(@viewer.registry_id)
    @viewer.destroy
    flash[:notice] = "Viewer deleted"
    respond_to do |format|
      format.html { redirect_to registry_path(@viewer_registry) }
      format.js {
        if params[:template] == "viewer"
          render :action => "viewer_destroy.js.rjs"
        else
          render :action => "destroy.js.rjs"
        end
      }
    end
  end

So I've got 2 RJS files instead, and different ones are used based on the incoming template name. Yay!

Jty.tan