Domanda

Ok, so I have ?document_key=2343t8ujjf being appended to my redirected url(index of my Partners Controller) after user signs an e-document.

I am storing this into a class variable in the index method. However, I have to use a different method (here, it's show) to actually store the new key in my db because I have to use @partner = Partner.find(params[:id]) before I can update its attributes and I cannot call @partner = Partner.find(params[:id]) in index because no id gets passed through the server for that method.

Of course, I can replace params[:id] with an id that currently exists but that would be really bad practice. I don't want to call show manually from a button but even calling it from index automatically, it's requiring an id.

Is there a way to get around this?

class PartnersController < ApplicationController
# GET /partners
# GET /partners.json

def index

  @@agreement_key = params[:documentKey]
  logger.info "Key: #{@@agreement_key}"

  @partners = Partner.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @partners }
  end

  show # could also just paste parts of show into index but same problem
       # other than being able to then use just ivar.

end

# GET /partners/1
# GET /partners/1.json
def show
  @partner = Partner.find(params[:id])
     logger.info "Key: #{@@agreement_key}"
     @partner.update_attributes(
        {
          :agreement_key => @@agreement_key
        }
      )
  respond_to do |format|
    format.html do 
      redirect_to signed_path( partner_id: @partner.id )
    end
    format.json { render json: @partner }
  end
end
È stato utile?

Soluzione

You should store the value in session.

Using a class variable will give you some troubles, i.e. when there is more than one user, when the user connects to different workers on subsequent requests, when you restart your server between the requests.

You can read more about session storage here: http://guides.rubyonrails.org/action_controller_overview.html#session

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top