简体   繁体   中英

Form_for - redirect with errors

I use form_for(@user) in a view settings#info :

# ./views/settings/info.html.erb

<%= form_for(@user) do |f|  %>

    <% if @user.errors.any? %>
        <% @user.errors.full_messages.each do |msg| %>
            <li>* <%= msg %></li>
        <% end %>
    <% end %>
...
<% end %>


# ./controllers/users_controller.rb

  def update
    @user = User.find(current_user.id)
    if @user.update_attributes(params["user"])
        ....
    else
      render template: 'settings/info'
    end

  end

Everything is working great, errors are shown if there are some. But, because I pass some variables in info action of settings_controllers.rb, I need to change it to a redirect (with errors). How can I do that ?

Thanks in advance

在重定向的情况下,我通常使用rails flash对象并直接添加错误。

flash[:alert] = @user.errors.full_messages.join(' ')

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