假设您有一个 Web 应用程序,人们可以在其中提交链接、指向他们自己网站的链接以及指向他们不拥有的网站的链接。两种情况下的提交表单几乎相同,除非他们提交链接的域。
如果用户从他们自己注册的网站列表提交,他们会得到他们网站的下拉列表。
如果用户提交链接,则用户将域键入到我通过 attr_accessor 在“链接”模型中获取其值的字段中。 “链接”模型然后在该域上执行 find_or_create 方法。
现在我的解决方案并不令人满意:
当用户点击“提交链接”时,我将 ?other=prompt 放入 url 中,然后有这种条件:
<% if params[:other] == "prompt" %>
<div class="prompt_form">
<h2>This link is for:</h2>
<span class="blue_button"><%= link_to "My Website", new_link_path%></span> <span class="blue_button"><%= link_to "Another User's Site", new_link_path(:other => "yes")%></span>
</p>
当用户在表单中的两个选项之间做出选择时,呈现方式不同:
<% if params[:other] == "yes" || current_user == nil %>
<strong>Website Domain:</strong><br />
<%= f.text_field :unclaimed_domain %><br />
<% elsif current_user %>
<% if current_user.websites.size > 1 %>
<p>
<strong>Website Domain:</strong><br />
<%= f.collection_select :website_id, current_user.websites.valid, :id, :domain, {:include_blank => true} %> <%= error_message_on :link, :website %><br />
<%= link_to "Add a Website", new_website_path %>
</p>
<% else %>
<p>
<strong>Website Domain:</strong><br />
<%= f.collection_select :website_id, current_user.websites.valid, :id, :domain %><br />
<%= link_to "Add a Website", new_website_path %>
</p>
<% end %>
这样做的问题是,如果用户犯了错误并且某些验证失败,“render :action => 'new'”代码就会被执行,并且参数中的所有信息都会丢失。有没有办法保留这些信息,或者可能有一种完全不同的方式来做到这一点?
最佳答案
我对你的问题只有模糊的理解,但似乎你想要这个:
<input name="other" value="<%= params[:other] %>" type="hidden">
这样,当前值
other
将被重新提交,如果验证失败,它仍然可以访问。
关于ruby-on-rails - rails : Keep params after a submit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3401671/