简体   繁体   中英

using render or redirect_to

I have a form

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

if I use render, it update my text_field_tag but it change my url to localhost:3000/update. redirect_to don't update my text and I don't know why. How can I use redirect_to to update text_field_tag

If you use redirect_to then you will need to pass the parameters with the request since the redirect_to goes to a different URL. Some good background information on redirect and render is given in the SO question Are redirect_to and render exchangeable?

redirect_to will make the browser request a new page(here is your max_channel), and current context will be lost. That's why you cannot update @text in the max_channel page.

while render will use current context to render max_channel page.

you can get more from http://guides.rubyonrails.org/layouts_and_rendering.html#using-redirect-to .

You can save @text to somewhere(say your sqlite ) and retrieve it in the method corresponding to max_channel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM