link_to更新(无表格) [英] link_to update (without form)

查看:80
本文介绍了link_to更新(无表格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个链接来更新资源,而不使用HTML表单。

I want a link to update a resource, without using an HTML form.

路由:

resources :users do
  resources :friends
end    

耙路:

 user_friend GET /users/:user_id/friends/:id(.:format){:action=>"show", :controller=>"friends"}
             PUT /users/:user_id/friends/:id(.:format){:action=>"update", :controller=>"friends"}

I想要使用put来通过一个简单的链接来更新朋友,例如:

I want to use the put to update a friend by a simple link, something like this:

<%= link_to "Add as friend", user_friend_path(current_user, :method=>'put') %>

但是当我单击链接时,它会尝试进入显示操作。

But when I click the link, it tries to go into the show action.

正确的方法是什么?

推荐答案

link_to "Add as friend", user_friend_path(current_user, @friend), :method=> :put

将插入属性 data-method设置为 put的链接,这将反过来被Rails javascript拾取,并变成了幕后的形式……我想这就是您想要的。

Will insert a link with attribute 'data-method' set to 'put', which will in turn be picked up by the rails javascript and turned into a form behind the scenes... I guess that's what you want.

您应该考虑使用:post,因为您似乎正在两个用户之间创建一个新链接,而不是对其进行更新。

You should consider using :post, since you are creating a new link between the two users, not updating it, it seems.

这篇关于link_to更新(无表格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆