Ruby-on-rails Rails redirect_to 发布方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/997956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:21:41  来源:igfitidea点击:

Rails redirect_to post method?

ruby-on-railsruby

提问by LucaM

    redirect_to :controller=>'groups',:action=>'invite'

but I got error because redirect_to send GET method I want to change this method to 'POST' there is no :method option in redirect_to what will I do ? Can I do this without redirect_to.

但我收到错误,因为redirect_to 发送GET 方法我想将此方法更改为“POST”,redirect_to 中没有:method 选项,我该怎么办?我可以在没有redirect_to的情况下做到这一点吗?

Edit:

编辑:

I have this in groups/invite.html.erb

我在 groups/invite.html.erb 中有这个

<%= link_to "Send invite", group_members_path(:group_member=>{:user_id=>friendship.friend.id,  :group_id=>@group.id,:sender_id=>current_user.id,:status=>"requested"}), :method => :post %>

This link call create action in group_members controller,and after create action performed I want to show groups/invite.html.erb with group_id(I mean after click 'send invite' group_members will be created and then the current page will be shown) like this:

此链接调用 group_members 控制器中的创建操作,执行创建操作后,我想显示带有 group_id 的 groups/invite.html.erb(我的意思是单击“发送邀请”后将创建 group_members,然后将显示当前页面),例如这个:

redirect_to :controller=>'groups',:action=>'invite',:group_id=>@group_member.group_id

After redirect_to request this with GET method, it calls show action in group and take invite as id and give this error

在使用 GET 方法redirect_to 请求此之后,它在组中调用 show 操作并将邀请作为 id 并给出此错误

Couldn't find Group with ID=invite

My invite action in group

我在群组中的邀请操作

    def invite
@friendships = current_user.friendships.find(:all,:conditions=>"status='accepted'")
@requested_friendships=current_user.requested_friendships.find(:all,:conditions=>"status='accepted'")
@group=Group.find(params[:group_id])
 end

The solution is I have to redirect this with POST method but I couldn't find a way.

解决方案是我必须使用 POST 方法重定向它,但我找不到方法。

Ugly solution: I solved this problem which I don't prefer. I still wait if you have solution in fair way.

丑陋的解决方案:我解决了这个我不喜欢的问题。如果您有公平的解决方案,我仍然会等待。

My solution is add route for invite to get rid of 'Couldn't find Group with ID=invite' error.

我的解决方案是为邀请添加路由以摆脱“无法找到 ID=invite 的组”错误。

in routes.rb

在routes.rb

   map.connect "/invite",:controller=>'groups',:action=>'invite'

in create action

在创造行动

   redirect_to "/invite?group_id=#{@group_member.group_id}"

I call this solution in may language 'amele yontemi' in english 'manual worker method' (I think).

我将此解决方案用英语称为“手工工人方法”(我认为)。

采纳答案by Steve Madsen

It sounds like you are getting tripped up by how Rails routing works. This code:

听起来你被 Rails 路由的工作方式绊倒了。这段代码:

redirect_to :controller=>'groups',:action=>'invite',:group_id=>@group_member.group_id

creates a URL that looks something like /groups/invite?group_id=1.

创建一个类似于/groups/invite?group_id=1.

Without the mapping in your routes.rb, the Rails router maps this to the showaction, not invite. The invitepart of the URL is mapped to params[:id]and when it tries to find that record in the database, it fails and you get the message you found.

如果没有你的映射routes.rb,Rails的路由器映射此为show行动,而不是inviteinviteURL的一部分被映射到params[:id],当它尝试在数据库中找到该记录时,它失败了,您会收到找到的消息。

If you are using RESTful routes, you already have a map.resourcesline that looks like this:

如果您使用的是 RESTful 路由,那么您已经有map.resources一行如下所示:

map.resources :groups

You need to add a custom action for invite:

您需要为以下对象添加自定义操作invite

map.resources :groups, :member => { :invite => :get }

Then change your reference to params[:group_id]in the #invitemethod to use just params[:id].

然后将您params[:group_id]#invite方法中的引用更改为使用 just params[:id]

回答by LucaM

The answer is that you cannot do a POST using a redirect_to.

答案是您不能使用redirect_to.

This is because what redirect_todoes is just send an HTTP 30x redirect header to the browser which in turn GETs the destination URL, and browsers do only GETs on redirects

这是因为redirect_to所做的只是向浏览器发送一个 HTTP 30x 重定向标头,浏览器反过来获取目标 URL,而浏览器只对重定向执行 GET

回答by sirneb

I found a semi-workaround that I needed to make this happen in Rails 3. I made a route that would call the method in that controller that requires a post call. A line in "route.rb", such as:

我找到了一个半解决方法,我需要在 Rails 3 中实现这一点。我创建了一个路由,该路由将调用需要后期调用的控制器中的方法。“route.rb”中的一行,例如:

match '/create', :to => "content#create"

It's probably ugly but desperate times call for desperate measures. Just thought I'd share.

这可能是丑陋的,但绝望的时代需要采取绝望的措施。只是想我会分享。

回答by yaro

The idea is to make a 'redirect' while under the hood you generate a form with method :post.

这个想法是在使用方法:post 生成表单时进行“重定向”。

I was facing the same problem and extracted the solution into the gem repost, so it is doing all that work for you, so no need to create a separate view with the form, just use the provided by gem function redirect_post()on your controller.

repost遇到了同样的问题并将解决方案提取到 gem 中,所以它正在为您完成所有工作,因此无需使用表单创建单独的视图,只需使用redirect_post()控制器上的 gem 函数提供。

class MyController < ActionController::Base
...
  def some_action
    redirect_post('url', params: {}, options: {})
  end
...
end

Should be available on rubygems.

应该在 ruby​​gems 上可用。