Reputation: 13
I have tried and tried to find the answer elsewhere, but I just cannot resolve this problem.
I have a model call BidSignal that is related to a model called BuySignal. A BuySignal has many BidSignals and a BidSignal belongs to a BuySignal.
When I create a new BidSignal using the new method in the BidSignal controller I want to capture the BuySignal that it relates to, so I can save the relation in the BidSignal create method. I'm currently doing this by passing the buysignal_id to BidSignal controller on a call to the new method. But when I come to create the BidSignal record and try to recall the buysignal_id to find the BuySignal record, I get an error - Couldn't find Buysignal without an ID
Bid Signal New Method
def new
@buy_signal_idx = params[:buysignal_id]
@bid_signal = BidSignal.new
end
Bid Signal Create Method
def create
@a_buysignal = Buysignal.all
@a_buysignal = @a_buysignal.find(@buy_signal_idx)
@a_bid_signal = @a_buysignal.bid_signals.build(bid_signal_params)
if @a_bid_signal.save
flash[:success] = "Bid Signal Successfully Created!"
redirect_to @a_bid_signal
else
render action: 'new'
end
end
However, if I replace: @a_buysignal = @a_buysignal.find(@buy_signal_idx)
with:
@a_buysignal = @a_buysignal.find(300)
It all works fine ( except of course all my bid signals are being related to buy signal with id 300 ).
It's as if the @buy_signal_idx is not accessible within the create method. I have tried abstracting this out so that new calls a class to set a variable for the bid signal through it's own new method and then use a getter to return the buy_signal_idx in the bid signal create method. But i get the same result.
I think I get that if I initiate an instance variable in a method that variable should be accessible by other methods in that object instance - but it's not working.
I know i'm being stupid and missing something fundamental - but what is it ?
Upvotes: 1
Views: 729
Reputation: 44715
Every time new request comes in, rails instantiate new instance of the controller class - hence the instance variables set in previous request are no longer available (controller object holding this variable is destroyed by now). This is a result of web being stateless - the only ways to preserve any form of data between two separate requests is to store it within the session or to send it with every request (not really storing the variable, but you know what I mean)
The best choice depends really on your routes here. If you are using nested resources, than params[:buysignal_id]
is passed as a part of url (not the query string) and hence it is also accessible within your create action. So you can simply do:
# create action
@a_buysignal = Buysignal.find(params[:buysignal_id])
If it is part of a query string, then I would do:
# new action
@buy_signal_idx = session[:byusignal_id] = params[:buysignal_id]
# create action
@a_buysignal = Buysignal.find(session[:buysignal_id])
Another solution is to add a hidden field with a params[:buysignal_id]
value to your form and use it in your create action, but i think it is a little bit cumbersome.
Upvotes: 2