Tag: params

数组不会持久保存到数据库

我正在尝试在我的数据库中存储一个数组(:vehicles)。 该arrays由用户通过复选框选择的3种可能的车辆组成。 ‘:vehicles’属性类型是’text’,我使用此代码使其成为一个数组: serialize :vehicles, Array 如果我发送参数,它会在数组中获取这些值。 “vehicles”=>[“0”, “1”, “2”], “commit”=>”Sign up!”, “controller”=>”registrations”, “action”=>”create” 问题是当我尝试保存时,此数组不存储在数据库中。 在数据库中,代码如下所示: vehicles: [] 控制器代码: def create @student = Student.create(student_params) if @student.save! redirect_to @student else render :new end end 学生参数: def student_params params.require(:student).permit(availabilities_attributes: [ :id, :day, :available]) end 任何人都知道如何将这些值放入此数组中? 谢谢!

如何绕过default_url_options?

在我上一个Rails项目中,我不得不覆盖application_controller.rb default_url_options ,以便始终在每个请求中传递一个参数,如下所示: def default_url_options { :my_default_param => my_param_value } end 现在每个url助手都会在每个生成的url末尾附加my_default_param。 例如, user_url(@current_user)生成一个URL,如: http://www.example.com/users/1?&my_default_param=my_param_value 但有时我需要生成没有my_default_param的url 。 是否有一些选项可以传递给url帮助器,以便user_url(@current_user, some_option: some_option_value )仅返回: http://www.example.com/users/1?&my_default_param=my_param_value ? PN:我已经尝试过:overwrite_params=> { }但它不起作用,可能是因为它在default_url_options之前进行了评估。 如果没有,是否有另一种方法可以在没有附加my_default_param的情况下获取@current_userurl? 谢谢。

如何从LinkedIn解析Mash以创建Ruby对象

我使用pengwynn的LinkedIn gem来获取LinkedIn的身份validation。 一切正常,我在回调中得到一个Mash,如下所示: #<LinkedIn::Mash all=[#<LinkedIn::Mash company=# id=2851554 is_current=true start_date=# summary=”” title=”Boss”>] total=1> 我怎样才能将它解析为类似于Rails参数的东西,以便从中创建一个新对象? 谢谢。

Rails:通过Ajax传递Params

我需要通过javascript将params传递回服务器。 目前,我将它们传递给javascript,如下所示: sendParams(“”); 然后像这样发送回来: function sendParams(q){ $.ajax({ url: ‘/mymodel/myaction’, type: ‘post’, data: {‘q’:q}, contentType: ‘json’ }); } 在我的控制器中,我试着像使用任何其他参数一样使用它们: MyModel.where(params[:q]) 但即使firebug在POST标签中显示了这个内容,params仍然是空的: q=%7B%26quot%3Bc%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Ba%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Btitle%26quot%3B%7D%7D%2C%26quot%3Bp%26quot%3B%3A%26quot%3Bcont%26quot%3B%2C%26quot%3Bv%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bvalue%26quot%3B%3A%26quot%3B2%26quot%3B%7D%7D%7D%7D%2C%26quot%3Bs%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Bvotes_popularity%26quot%3B%2C%26quot%3Bdir%26quot%3B%3A%26quot%3Bdesc%26quot%3B%7D%7D%7D 知道为什么这些信息没有被where子句处理? 我该怎么做才能使params Rails再次可读? 更新: Started POST “/publications/search?scroll=active&page=6” for 127.0.0.1 at 2013-0 2-12 22:55:24 -0600 Processing by PublicationsController#index as */* Parameters: {“scroll”=>”active”, “page”=>”6”} 更新2: 问题显然源于contentType 。 当我删除它,然后q作为Rails参数发送。 不幸的是, q仍然是JSON,导致错误: undefined method `with_indifferent_access’ for # 如何将JSON转换为params哈希?

是否可以为在params中传递的提交按钮添加值?

我正在尝试向我的应用添加“预览”按钮/页面。 有没有办法为我的“post”和“preview”提交按钮添加不同的值,这些按钮在params散列中传递,以便我可以检查在控制器中按下了哪一个并相应地渲染视图? 这是最好的方法吗?

Rails 3:使用AJAX请求更新URL参数

我有一个filter和一个产品列表(id,name,creation_date)。 我可以按id,name或creation_date进行过滤。 使用AJAX请求我更新内容div …但显然URL不会更改。 如何将params附加到URL? 例如: localhost:3000/dashboard/catalog?name=radio&?date_creation=23-06-2013 我知道history.pushState(html5)存在…但我需要我的应用程序在IE9等html4浏览器中运行。 我尝试使用Wiselinks( https://github.com/igor-alexandrov/wiselinks ),它使用History.js,但它没有使用AJAX请求。 有任何想法吗? 谢谢

将ruby hash转换为URL查询字符串…没有那些方括号

在Python中,我可以这样做: >>> import urlparse, urllib >>> q = urlparse.parse_qsl(“a=b&a=c&d=e”) >>> urllib.urlencode(q) ‘a=b&a=c&d=e’ 在Ruby [+ Rails]中,我无法弄清楚如何在没有“滚动我自己”的情况下做同样的事情,这看起来很奇怪。 Rails方式对我不起作用 – 它为查询参数的名称添加方括号,另一端的服务器可能支持也可能不支持: >> q = CGI.parse(“a=b&a=c&d=e”) => {“a”=>[“b”, “c”], “d”=>[“e”]} >> q.to_params => “a[]=b&a[]=c&d[]=e” 我的用例很简单,我希望使用URL的query-string部分中的某些值的值。 依靠标准库和/或Rails似乎很自然,写下这样的东西: uri = URI.parse(“http://example.com/foo?a=b&a=c&d=e”) q = CGI.parse(uri.query) q.delete(“d”) q[“a”] << "d" uri.query = q.to_params # should be to_param or to_query instead? puts Net::HTTP.get_response(uri) 但是,只有结果URI实际上是http://example.com/foo?a=b&a=c&a=d […]

如何检查params 是否为零?

我尝试了很多人的代码 – 如何测试rails中是否存在参数 ,但它不起作用(): if ( params.has_key?([:start_date]) && params.has_key?([:end_date]) ) 我想,那是因为复杂的参数,如果我写这个: if ( params.has_key?([:report][:start_date]) && params.has_key?([:report][:end_date]) ) 给我错误 can’t convert Symbol into Integer 这也行不通: if ( params[:report][:start_date] && params[:report][:end_date] ) 给我错误: undefined method `[]’ for nil:NilClass 它总是进入其他声明。 这是我的参数: report: start_date: 01/08/2012 end_date: 10/08/2012 有人能帮我吗 ?

Ruby on Rails:params是零。 未定义的方法`’为nil:NilClass

我试图访问我所做的搜索页面时收到错误。 问题似乎是我试图在没有任何空白时删除空白。 我的搜索function有效,但是当technols为零时我无法处理这个案例。 这是我的错误: NoMethodError in ProjectsController#search undefined method `[]’ for nil:NilClass 在我的项目控制器中,第26行: tech_ids = params[“technols”][“id”].reject(&:blank?). 这是我的搜索操作: def search tech_ids = params[“technols”][“id”].reject(&:blank?) @search = params[:client], params[:industry], params[:role], tech_ids, params[:business_div], params[:project_owner], params[:status], params[:start_date_dd], params[:start_date_A], params[:start_date_B], params[:keywords] @project_search = Project.search(*@search).order(sort_column + ‘ ‘ + sort_direction).paginated_for_index(per_page, page) @search_performed = !@search.reject! { |c| c.blank? }.empty? @project = Project.new(params[:project]) @all_technols […]

如何隐藏我传输给方法的params(比如form_for似乎这样做)?

我一直在寻找几个小时,但没有发现任何有用的东西。 我想做的事: 我需要调用check_login -Method(如下所示),它需要参数。 redirect_to check_login_users_url( :user => {:name => input[1], :password => input [2] }, :stylesheet => ‘scaffold’, :method => :get) 关键是这些参数在方法调用中发送,如下面的“重定向到” – 线。 Processing ApplicationController#execute(for 127.0.0.1 at 2009-12-19 00:28:40) [POST] Parameters: {“command”=>{“line”=>”log dodo wg”}, “authenticity_token”=> } Redirected to http://localhost:3000/benutzer/check_login?method=get&stylesheet=scaffold&user%5Bname%5D=dodo&user%5Bpassword%5D=wg Completed in 9ms (DB: 0) | 302 Found [http://localhost/execute] 我想阻止rails将params放入url并将其隐藏。 当我发送一个用form_for创建的表单时,url中没有任何内容,所以我认为它必须是可能的。 请告诉我该怎么做。 试过的步骤 我尝试过不同的“html-verbs”:get,put,post […]