Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

ruby on rails - how to pass a variable with redirect_to?

In my controller destroy function, I would like to redirect to index after the item deleted, and I would like to pass a variable called 'checked' when redirect:

def destroy
    @Car = Car.find(params[:id])
    checked = params[:checked]

    if @car.delete != nil

    end

    redirect_to cars_path #I would like to pass "checked" with cars_path URL (call index)
end

how to pass this 'checked' variable with cars_path so that in my index function I can get it?? (cars_path calls index function)

def index
 checked = params[checked]
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you do not mind the params to be shown in the url, you could:

redirect_to cars_path(:checked => params[:checked])

If you really mind, you could pass by session variable:

def destroy
  session[:tmp_checked] = params[:checked]
  redirect_to cars_path
end

def index
  checked = session[:tmp_checked]
  session[:tmp_checked] = nil # THIS IS IMPORTANT. Without this, you still get the last checked value when the user come to the index action directly.
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...