加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails:提交后保留params

发布时间:2020-12-17 03:09:29 所属栏目:百科 来源:网络整理
导读:假设您有一个网络应用程序,人们可以在其中提交链接,自己网站的链接以及他们不拥有的网站链接.提交表单在两种情况下几乎相同,除非他们提交链接的域名. 如果用户从他们自己注册的网站列表中提交,他们将获得他们网站的下拉列表. 如果用户正在提交链接,则用户将
假设您有一个网络应用程序,人们可以在其中提交链接,自己网站的链接以及他们不拥有的网站链接.提交表单在两种情况下几乎相同,除非他们提交链接的域名.

如果用户从他们自己注册的网站列表中提交,他们将获得他们网站的下拉列表.

如果用户正在提交链接,则用户将域键入到字段中,该字段的值通过attr_accessor在“链接”模型中获得.然后,“链接”模型在该域上执行find_or_create方法.

现在我的解决方案并不令人满意:
当用户点击“提交链接”时,我将?other =提示符放入网址,然后有这种条件:

<% 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,:domain %><br />
                        <%= link_to "Add a Website",new_website_path %>
                    </p>
                <% end %>

这样做的问题是,如果用户发出错误并且某些验证失败,则会执行“render:action =>’new’”代码并且params中的所有信息都会丢失.有没有办法保持这些信息或者可能采用不同的方式来完成这项工作?

解决方法

我对你的问题只有模糊的理解,但似乎你想要这个:

<input name="other" value="<%= params[:other] %>" type="hidden">

这样,将重新提交其他的当前值,如果验证失败,它仍然可以访问.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读