0

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!

4

1 に答える 1

0

そうです、destroy 関数を次のように作り直しました。

  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

そのため、代わりに 2 つの RJS ファイルがあり、受信したテンプレート名に基づいて異なるファイルが使用されます。わーい!

于 2010-07-16T05:50:09.323 に答える