Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 基于helper变量将link_更改为path_Ruby On Rails - Fatal编程技术网

Ruby on rails 基于helper变量将link_更改为path

Ruby on rails 基于helper变量将link_更改为path,ruby-on-rails,Ruby On Rails,我有一个助手方法,我想用于几个模型,以避免重复相同的代码多次 def link_to_follow(instance:instance,type:type) link_to('Unfollow', unfollow_books_path(id: instance.id), method: :post, id: "unfollow_link_#{instance.id}", remote: true) end 我如何做才能使如果

我有一个助手方法,我想用于几个模型,以避免重复相同的代码多次

def link_to_follow(instance:instance,type:type)
  link_to('Unfollow', unfollow_books_path(id: instance.id), 
          method: :post, 
          id: "unfollow_link_#{instance.id}",
          remote: true)
end

我如何做才能使如果我将
键入:'magazine'
作为变量传递,那么
unfollow\u books\u path
将变成
unfollow\u magazies\u path

def link_to_follow(instance:instance, type:type)
  link_to(
    'Unfollow', 
    send("unfollow_#{type.pluralize}_path", instance), 
    method: :post, 
    id: "unfollow_link_#{instance.id}",
    remote: true
  )
end  
我想你也可以做到:

def link_to_follow(instance:instance)
  link_to(
    'Unfollow', 
    send("unfollow_#{instance.class.name.underscore.pluralize}_path", instance), 
    method: :post, 
    id: "unfollow_link_#{instance.id}",
    remote: true
  )
end

啊!!这是有道理的。谢谢!