Ken*_*and 5 forms ruby-on-rails
假设您有一个网络应用程序,人们可以在其中提交链接,自己网站的链接以及他们不拥有的网站链接.提交表单在两种情况下几乎相同,除非他们提交链接的域名.
如果用户从他们自己注册的网站列表中提交,他们将获得他们网站的下拉列表.
If the user is submitting a link the user types the domain into a field whose value I get in the "link" model through an attr_accessor. The "link" model then executes a find_or_create method on that domain.
Right now my solution is unsatisfactory: When the user clicks "Submit Link" I put ?other=prompt into the url and then have this kind of conditional:
<% 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>
Run Code Online (Sandbox Code Playgroud)
When the user makes a choice between the two options in the form renders differently:
<% 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 %>
Run Code Online (Sandbox Code Playgroud)
The problem with this is that if the user makes an error and fails some validations, the "render :action => 'new'" code gets executed and all the info in the params gets lost. Is there a way to keep that info or perhaps a different way to do this altogether?
I have only vague understanding of your problem, but it seems you want this:
<input name="other" value="<%= params[:other] %>" type="hidden">
Run Code Online (Sandbox Code Playgroud)
This way, current value of other will be resubmitted, and, if validation fails, it'll still be accessible.
| 归档时间: |
|
| 查看次数: |
4237 次 |
| 最近记录: |