How to avoid variable been reset

I have a form that send an AJAX request to the controller, the action
just
instantiate an object so I could send data back to the view, but every
time
I hit the action the variable reset is there a way to avoid this
behaviour.

This is my code inside my SaleController :

def getquote
@line_item = LineItem.new(
params.require(:line_item).permit(:project_name, :pages, :quantity,
:paper_id, :product_id, :plastic_id, :discount, :quote_before_discount,
:duplex, :cost, :width, :height, :extras) )
@number = params[:index].match(/\d+/)[0]
@sale = Sale.new
@sale.line_items << @line_item
end

What I want is @sale not to be reset every time I hit the action, I
thought
about @sale ||= Sale.new but that didn’t work.

Any help would be appreciated.

Thanks.

On May 14, 2014, at 5:42 AM, Gustavo C. wrote:

What I want is @sale not to be reset every time I hit the action, I thought
about @sale ||= Sale.new but that didn’t work.

Any help would be appreciated.

Browsers don’t work this way, not without some sort of token to maintain
state. If you have a session, and you use that to persist the sale
across requests, then you can do this. I suspect though that your real
problem is that you are trying to use one end-point for the :new,
:create, and :update methods. That’s not going to end well, or at the
least, it’s going to be a hairy bit of code. Try to re-think your flow
to the point where it would work without Ajax, then layer Ajax on top of
that. That’s the way to success in any multi-step Web application of any
kind.

Walter

Thanks Walter for the quick answer.

I kind of new to ajax, but what I’m trying to do is in a form when
selecting data from the select drop down I would like to change with
ajax
some div that contain information about the sale, like price, quote
etc…
The problem is when I try to get the info for the sale which is reset
every
time I change the state in the select drop down.

I don’t know if I have explain myself but I hope you could give a hint.

Thanks

Thanks Walter that sounds pretty good I think I’m going to do that.

I will store the hole Sale in the session until it gets created.

Thanks.

Gustavo

On May 14, 2014, at 8:35 AM, Gustavo C. wrote:

Thanks Walter for the quick answer.

I kind of new to ajax, but what I’m trying to do is in a form when selecting
data from the select drop down I would like to change with ajax some div that
contain information about the sale, like price, quote etc…
The problem is when I try to get the info for the sale which is reset every time
I change the state in the select drop down.

I don’t know if I have explain myself but I hope you could give a hint.

What you need to do is the first time the user requests anything from
the site, set a session cookie. Then make sure that you check that
session cookie each subsequent time and re-hydrate their one and only
request. Without knowing much about your application, I can’t really
give you specific answers, but try just putting a hidden field in your
form with the user id, and store that in the session on the server site.
Your method would begin with this sort of line:

unless @sale = Sale.find_by_id session[:sale_id]
@sale = Sale.create()
session[:sale_id] = @sale.id
end

But this gets you a bunch of empty sales in your database if nobody
completes the order process. Another way to do this is to put the actual
sale in the session until it gets persisted, and then store the id
thereafter.

Walter

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs