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
啊!!这是有道理的。谢谢!