表单提交后的 Rails 再次重定向到空表单页面

jam*_*mes 4 html forms post ruby-on-rails http-redirect

我正在构建一个带有简单联系表单的页面。用户提交表单后,我希望它重定向回完全相同的页面,然后显示成功消息。但我不使用节目的原因是因为我想要一个全新的联系表(不是他们已经提交的),以防他们想提交另一个请求。但是,redirect_to 似乎不起作用。

谢谢您的帮助!

我的控制器(请注意,我也尝试过使用 render 而不是 redirect_to)

def create
    @contact= Contact.new(secure_params)
    if @contact.save
      flash[:success] = "Thanks! I'll be in touch soon!"
      redirect_to 'new'
    else
      render 'new'
    end
end
Run Code Online (Sandbox Code Playgroud)

我的看法

<body>
<h1> opening text </h1>

<!--prints success message, making it a hash just in case other messages need to be added in the future -->
<% flash.each do |key, value| %>
    <div class="alert alert-<%= key %>"><%= value %></div>
<% end %>


<%= form_for @contact do |f| %>
<!--form code below -->
Run Code Online (Sandbox Code Playgroud)

San*_*osh 5

将其更改为

if @contact.save
  flash[:success] = "Thanks! I'll be in touch soon!"
  redirect_to :action => 'new'
else
  render :action => 'new'
end
Run Code Online (Sandbox Code Playgroud)

redirect_to 期望 url/相对路径作为字符串,或指向 url 的哈希

例如:

"/user/10"
"http://localhost:3000/users/new" 
or  {:controller => 'users', :action => 'new'}
Run Code Online (Sandbox Code Playgroud)

但当你给予时redirect_to 'new',它正在寻找'/new'