Define a params in a variable

103 views Asked by At

I would know how define a params in a variable to use it in another method

In my controller i have result page and contact page, i want store the search params from result page in variables and get them in my contact page method to not duplicate form fields

My result page

def result
    if params[:room_type].present? && params[:location].present? && params[:nb_piece].present?
          @biens = Bien.near(params[:location], 1, units: :km).where(room_type: params[:room_type], nb_piece: params[:nb_piece])
    end
       @users = User.where(id: @biens.reorder(:user_id).pluck(:user_id), payer: true) || User.where(id: @biens.reorder(:user_id).pluck(:user_id), subscribed: true)
end 

I want store this params in my other method,like that i will need to ask only email and phone in my form

def contact
    wufoo(params[:location], params[:room_type], params[:nb_piece], params[:email], params[:phone])
end 

My wufoo

def wufoo(adresse, type, pieces, email, phone)
     require "net/http"
     require "uri"
     require "json"

     base_url = 'https://wako94.wufoo.com/api/v3/'
     username = 'N5WI-FJ6V-WWCG-STQJ'
     password = 'footastic'

     uri = URI.parse(base_url+"forms/m1gs60wo1q24qsh/entries.json")

     request = Net::HTTP::Post.new(uri.request_uri)
     request.basic_auth(username, password)

     request.set_form_data(
       'Field7' => adresse,
       'Field9' => type,
       'Field12' => email,
       'Field11' => phone,
       'Field8' => pieces
     )

      response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme =='https'){
           |http|http.request(request)
      }

      puts JSON.pretty_generate(JSON[response.body])
end
1

There are 1 answers

1
roo On

It depends on how a user goes from search to contact. I assume that the contact form is linked off the search, and that they want to contact you regarding the information in the last search.

A simple method here would be to store the last search within the session, and just reference that.

def search
  store_params_in_session
  # .. your search logic here
end

def contact
  last_search = session[:last_search]
  if last_search.blank?
    # .. some error handling if no search is available
    return
  end

  wufoo(last_search[:location], #.. you get the idea
end

private

def store_params_in_session
  session[:last_search] = {
    location: params[:location],
    # .. more params here
  }