Rails 3: ":method =>:post"不起作用......应该“发布"时似乎是“获取" [英] Rails 3: ":method => :post" doesn't work... seems to be 'GET' when it should 'POST'

查看:27
本文介绍了Rails 3: ":method =>:post"不起作用......应该“发布"时似乎是“获取"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现友谊"在我的 Rails 3 应用程序中,如 Railscast 163:Self Referential Association 中所述

I'm trying to implement the "Friendship" in my Rails 3 app as described in Railscast 163:Self Referential Assosication

我已按照说明进行了所有设置.我使用的是使用 Authlogic 登录的基本用户模型,该模型工作正常.但是,当我尝试使用以下链接添加朋友时:

I have everything set up as described. I am using a basic user model that logis in with Authlogic which works fine. However when I try to add a friend using the following link:

<% for user in @users %>   
 <%=h user.username %> 
  <%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post %>     
<% end %>

我得到一个重定向到 http://localhost:3000/friendships?friend_id=2 和一个未知的动作 The action 'index' could not be found for FriendshipsController错误,没有进一步的解释.这特别奇怪,因为我有一个硬编码重定向回User#show"我当前用户的方法(即添加朋友后重定向回个人资料).

I get a redirect to http://localhost:3000/friendships?friend_id=2 and a Unknown action The action 'index' could not be found for FriendshipsController error with no further explanation. This is expecially strange since I have a hard coded redirect back to the "User#show" method for my current user (i.e. redirect back to profile after adding friend).

如果有帮助,这是我的friendships#create";方法:

If it helps, here is my "friendships#create" method:

def create  
  @friendship = current_user.friendships.build(:friend_id => params[:friend_id])  
  if @friendship.save  
    flash[:notice] = "Added friend."  
    redirect_to :controller => 'users', :action => 'show', :id =>'current_user'
  else  
    flash[:notice] = "Unable to add friend."  
    redirect_to :controller => 'users', :action => 'show', :id =>'current_user' 
  end  
end

知道是什么原因造成的吗?我发现有人在这里遇到了类似的问题,但我找不到明确的解决方法:Rails 3 和友谊模型

Any idea what could be causing this? I found someone having a similar problem here but I couldn't find a definite fix: Rails 3 and Friendship Models

预先感谢您的帮助!

~丹

推荐答案

我认为 link_to 将参数放入查询字符串中,因为它是使用 html 链接创建的,即使您放置 :method => :post 如果 js 被禁用.

I think that link_to put the arguments into the query string as it is creating with html link even if you put :method => :post if js is disabled.

您可以使用 javascript :onclik 事件模拟帖子.

you could simulte a post with javascript :onclik event.

无论如何,使用带有方法的link_to :post 通常是个坏主意.在 Rails 中,您可以使用 button_to 助手来完成此倾倒姿势并将其设置为链接样式.

aniway , use a link_to with method :post is generaly a bad idea. in rails you could use button_to helper for this pourpose and style it like a link.


在 Rails3 文档中,当使用 params :method=>:post

edit:
In Rails3 doc seems that link_to have to simulate the same behaviur of button_to when called with params :method=>:post

(动态创建一个 HTML 表单并立即提交表单).

(dynamically create an HTML form and immediately submit the form ).

但即使启用了 javascript,在 Rails 3.0.3 中对我来说也不是这样.我会调查.

but it's not true for me in Rails 3.0.3 even if javascript is enabled. I will investigate.

无论如何你应该为任何不是 GET 的东西使用按钮和表单;超链接故意不允许使用 GET 以外的方法

好的,rails3 不会创建内联表单来通过链接模拟发布请求.在 Rails3 中,一个 data-method=post 属性被添加到标签中,以便通过 javascript 函数对其进行操作.这样,如果 js 被禁用,请求会在 get 调用中优雅地降级.

edit2: ok, rails3 don't create a inline form for simulate the post request via link. in Rails3 a data-method=post attribute is added to the tag for manipulate it via javascript function. this way the request gracefully degradate in a get call if js is disabled.

这篇关于Rails 3: &quot;:method =&gt;:post"不起作用......应该“发布"时似乎是“获取"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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