开发者

How do I access the ID of a resource already loaded via AJAX if not through the URL params in Rails 3?

If I have a situation where paramters aren't passed explicitly via the URL because there are some ajax eleme开发者_JAVA技巧nts on the one page, how do I access an ID for an object loaded much earlier....for instance.

  1. User selects client.id=1
  2. User selects, project.id=3
  3. User selects, stages.id=9

But none of those IDs are passed through the URL.

In that example, by the time the user reaches step 3, I would like to access the client.id in the Client controller....how do I do that ?

If it were in the URL, I would just do client = Client.find(params[:id]), but if I do that now, by step 3, what gets returned as the ID parameter is the Stage ID - which is not what I want.

This is what their actions in their respective controllers look like:

Client Step Action

def step
    client = Client.find(params[:id])
    projects = client.projects
    respond_to do |format|
        #format.html { redirect_to("/") }
        format.html   { render :partial => "projects/show", :collection => projects, :as => :project, :layout => false, :status => :ok }
    end
  end

Project Step Action

def step
    project = Project.find(params[:id])
    stages = project.stages
    respond_to do |format|
        format.html   { render :partial => "stages/show", :collection => stages, :as => :stage, :layout => false, :status => :ok }
    end
  end


Stage Step Action


def step
    stage = Stage.find(params[:id])
    uploads = stage.uploads
    respond_to do |format|
        format.html   { render :partial => "uploads/show", :collection => uploads, :as => :upload, :layout => false, :status => :ok }
    end
  end


Would tracking the variable in the session object work? For example, in step 1. you do something like session["client_id] = @client.id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜