【问题标题】:using render or redirect_to使用 render 或 redirect_to
【发布时间】:2013-12-12 04:04:57
【问题描述】:

我有一个表格

<%= form_tag update_path do %>
         <%= text_field_tag :test_input, @text %>
         <%= submit_tag "update" %>
<% end %>

#mycontroller
def update
    @text= 'abcde'
    render  :max_channel #redirect_to :max_channel
end

如果我使用渲染,它会更新我的 text_field_tag,但会将我的 url 更改为 localhost:3000/update。 redirect_to 不更新我的文本,我也不知道为什么。如何使用 redirect_to 更新 text_field_tag

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    redirect_to 将使浏览器请求一个新页面(这是您的 max_channel),当前上下文将丢失。这就是您无法在max_channel 页面中更新@text 的原因。

    render 将使用当前上下文来呈现max_channel 页面。

    您可以从http://guides.rubyonrails.org/layouts_and_rendering.html#using-redirect-to获得更多信息。

    您可以将@text 保存到某个地方(比如您的sqlite),然后在max_channel 对应的方法中检索它。

    【讨论】:

      【解决方案2】:

      如果您使用redirect_to,那么您需要将参数与请求一起传递,因为redirect_to 会转到不同的URL。 SO问题Are redirect_to and render exchangeable?

      中提供了一些关于重定向和渲染的良好背景信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多