Tag: 参数

为什么这个Ruby方法通过引用传递它的参数

我回答了这个问题并偶然发现了一些奇怪的事情。 Ruby通过值传递参数,但变量本身就是引用。 那么为什么第一种方法似乎通过引用传递它的参数呢? require ‘set’ require ‘benchmark’ def add_item1!(item, list) list << item unless list.include?(item) end def add_item2(item, list) list |= [item] end def add_item3(item, list) set = Set.new(list) set << item list = set.to_a end array1 = [3,2,1,4] add_item1!(5, array1) p array1 # [3, 2, 1, 4, 5] array2 = [3,2,1,4] add_item2(5, array2) p […]

将参数传递给link_to方法

如何使用link_to方法通过MVC传递参数? 视图: 如何使用link_to方法来利用remove_tag操作? issues_controller.rb def remove_tag(parameter) @issue.remove_it(parameter) end issue.rb def remove_it(parameter) self.users.delete(User.find(parameter)) end

如何确定Ruby中是否传递了可选参数

如果我在Ruby中有以下方法: def foo(arg1, arg2 = “bar”) puts arg1 puts arg2 end 有没有办法确定用户是否在方法中传递了arg2的值? 显然我可以在方法中添加if arg2 == “bar” ,但这并不能解决用户自己手动传入”bar” 。 当然,我可以将默认设置为没有用户可以传递的内容,但是那很快就变得非常难看。 那里有什么优雅的东西吗?

如何定义Ruby中没有类型的参数的方法

如果我定义了一个方法: def weird_method(first_argument, second_argument) #code here end 所以这样我可以传递如下参数:1,:digit,’some’,“more”(Fixnum,Symbol,String等),但是如果我想传递这个: argument1,argument2 ,我的意思是没有类型,看起来像局部变量,但它们实际上不是。 我怎么能以某种方式接受它们并在方法体中使用它们? PS:我需要它来实现一个小的DSL,当blok通过时,它可以在.isntance_eval中做那种东西 Something.with_method do sum 1, 2 #(I can implement it) play volleyball #(I can NOT implement it) swim further #(I can NOT implement it) eat “a lot” #(I can implement it) end 编辑: 一切都可以继续排球 。 我只是举了一个例子。 sum,play,swim,eat是Something类中定义的方法。

如何允许嵌套属性的强参数?

当我尝试从表单接受嵌套属性时Unpermitted parameters: latitude, longitude, address我收到了一个Unpermitted parameters: latitude, longitude, address日志中的Unpermitted parameters: latitude, longitude, address错误。 确切的参数看起来像: { “widget”=> { “owner”=>”100”, “name”=>”Widget Co”, “locations_attributes” => { “0”=> { “latitude”=>”51.4794259”, “longitude”=>”-0.1026201″, “address”=>”123 Fake Street” } } }, “commit”=>”Create Supplier”, “action”=>”create”, “controller”=>”widgets” } 小部件具有多个位置,并且位置belongs_to小部件。 params是在widgets_controller中设置的,我认为它允许“0”下的所有内容,但似乎不是? def widget_params params.require(:widget).permit(:owner, :name, locations_attributes: [{“0” => []}]) end 是否有工作/更好的方式来接受这些参数? 谢谢

使用link_to方法rails 3将变量发送到控制器

我有一个link_to发送变量的问题。 使用form_for和submit按钮工作正常,但我需要使用link_to发送数据。 这适用于form_for: :post, :remote => true, :html => { :id => “#{@post.id}” }, :url => { :controller => “posts”, :action => “follow”, :post_id => post.id } do |f| %> 这不起作用:(: :post, :remote => true, :html => { :id => “#{@post.id}” }, :url => { :controller => “posts”, :action => “follow”, :post_id => post.id } […]

我可以告诉Ruby方法期望特定的参数类型吗?

def doSomething(value) if (value.is_a?(Integer)) print value * 2 else print “Error: Expected integer value” exit end end 我可以告诉Ruby方法某个参数应该是一个Integer,否则崩溃? 像Java一样。

预期的ProductField,得到arrays问题

我有一个rails 4应用程序,它有一个params块,看起来像: def store_params params.require(:store).permit(:name, :description, :user_id, products_attributes: [:id, :type, { productFields: [:type, :content ] } ]) end 但是我收到了错误: ActiveRecord::AssociationTypeMismatch in StoresController#create ProductField expected, got Array 我试图插入的参数如下: Parameters: {“utf8″=>”✓”, “store”=>{“name”=>”fdsaf”, “description”=>”sdfd”,”products_attributes”=>{“0″=>{“productFields”=>{“type”=>””, “content”=>””}}}}, “type”=>”Magazine”, “commit”=>”Create store”} 我的模特是 商店(有一个has_many :products ) 产品(有一个has_many :productFields和belongs_to :store ) ProductField(有一个belongs_to :product ) 我的观点如下: 然后product_fields部分:

Rails,PHP和参数

我在Rails工作,我需要调用PHP脚本。 我可以像这样连接到脚本: system(‘php public/myscript.php’) 但是我需要用它发送一些参数。 我怎么做? 谢谢

Ruby可选参数和多个参数

我试图将方法的第一个参数设置为可选,然后是任意数量的args。 例如: def dothis(value=0, *args) 我遇到的问题是,这似乎不太可能吗? 当我打电话给dothis(“hey”, “how are you”, “good”)我希望它将值设置为默认为0,但它只是使value=”hey” 。 有没有办法完成这种行为?