使用render或redirect_to

时间:2013-12-12 04:04:57

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

我有一张表格

<%= 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

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

2 个答案:

答案 0 :(得分:0)

如果您使用redirect_to,那么您需要传递带有请求的参数,因为redirect_to会转到不同的URL。有关重定向和渲染的一些有用的背景信息在SO问题Are redirect_to and render exchangeable?

中给出

答案 1 :(得分:0)

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

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

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

您可以将@text保存到某处(比如您的sqlite)并使用与max_channel对应的方法检索它。

相关问题