【问题标题】:Rails redirect_to post method?Rails redirect_to 发布方法?
【发布时间】:2009-06-15 19:38:55
【问题描述】:
    redirect_to :controller=>'groups',:action=>'invite'

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

编辑:

我在 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 %>

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

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

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

Couldn't find Group with ID=invite

我在群组中的邀请操作

    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

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

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

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

在 routes.rb 中

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

在创建动作中

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

我用英语“体力劳动者方法”(我认为)用五月语言“amele yontemi”将此解决方案称为。

【问题讨论】:

  • 你为什么要这样?你能提供更多背景信息吗?
  • 同意,我们需要更多关于您想要达到的目标的信息,然后才能真正给您一个好的答案。
  • redirect_to using POST in rails 的可能重复项

标签: ruby-on-rails ruby


【解决方案1】:

答案是您不能使用redirect_to 进行POST。

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

【讨论】:

  • 是的,我知道了,但是有没有办法解决我的问题,无论是否使用 redirect_to ?
【解决方案2】:

听起来您对 Rails 路由的工作方式感到困惑。这段代码:

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

创建一个类似于 /groups/invite?group_id=1 的 URL。

如果没有 routes.rb 中的映射,Rails 路由器会将其映射到 show 操作,而不是 invite。 URL 的 invite 部分映射到 params[:id],当它尝试在数据库中查找该记录时,它会失败,您会收到找到的消息。

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

map.resources :groups

您需要为invite 添加自定义操作:

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

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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

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

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

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

      应该在 ruby​​gems 上可用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        相关资源
        最近更新 更多