【问题标题】:error in redirection in ruby on railsruby on rails 重定向错误
【发布时间】:2014-04-25 19:58:24
【问题描述】:

我是 ruby​​ on rails 的新手。我有一个会员,每个会员都有很多健康记录,我为会员制作了一个按钮,可以在 HealthyGrocery/app/views/members/hub.html.erb 中添加新的健康记录,其中中心是主页 <%= button_to "new health record", redirect_to= 'http://localhost:3000/health_record/new', :method => "get" %> 通过此按钮,我想重定向到此链接“localhost:3000/health_record/new”,该链接位于 HealthyGrocery/app/views/health_record/new.html.erb 当我运行服务器并按下按钮制作新的健康记录时,它给了我这个错误screenshot of the error

【问题讨论】:

    标签: ruby-on-rails-3.2 mongoid


    【解决方案1】:
    redirect_to PATH
    

    您在按钮内部使用的命令完全脱离了上下文。您应该只在控制器内部使用它,作为根据操作结果重定向流下一步的方法。

    假设在论坛上成功发布帖子后,如果成功,您会将用户重定向到他的新主题。如果失败,您将再次将用户重定向到帖子表单,以便他可以更正它。

    您想要做的是使用自动 path_helper:

    new_health_record_path
    

    您还可以使用 link_to 而不是 button_to,并且不需要指定 HTTP 方法。

    最终结果:

    link_to('new health record', new_health_record_path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 2014-04-17
      • 1970-01-01
      相关资源
      最近更新 更多