2017-09-09 30 views
-1

我知道如何在一个变量定义PARAMS在另一种方法定义PARAMS在变量

在我的控制器中我已经结果页面和联系人页面,我想存储从结果页面搜索PARAMS在使用它变量,并让他们在我的联系人页面的方法不重复的表单域

我的结果页面

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 

我想保存这PARAMS在我的其他方法,比如我需要询问只电子邮件和电话中我的表格

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

我的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

这取决于用户从搜索如何去联系。我假定联系表单与搜索关联,并且他们想要就上次搜索中的信息与您联系。

这里的一个简单方法是将最后一次搜索存储在会话中,并引用它。

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 
    } 
+0

感谢您的帮助,我尝试了您的解决方案,但我没有成功将我的参数存储在会话中 –