【问题标题】:Define a params in a variable在变量中定义参数
【发布时间】:2017-09-09 21:02:32
【问题描述】:

我会知道如何在变量中定义参数以在另一个方法中使用它

在我的控制器中,我有结果页面和联系页面,我想将结果页面中的搜索参数存储在变量中,并在我的联系页面方法中获取它们,以免重复表单字段

我的结果页面

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 

我想将此参数存储在我的其他方法中,这样我只需要在我的表单中询问电子邮件和电话

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

我的五福

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

【问题讨论】:

    标签: ruby-on-rails ruby wufoo


    【解决方案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
      }
    

    【讨论】:

    • 感谢您的帮助,我尝试了您的解决方案,但我没有成功将我的参数存储在会话中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    相关资源
    最近更新 更多