Rails: save parameters after submit

Say you have a web application where people can send links, links to their own websites and links to a website that they don’t have. The submission form is almost the same in both cases, except when they transfer the link domain.

If a user leaves the list of their registered sites, they will receive a drop-down list of their websites.

If the user sends a link, the user enters the domain into the field, the value of which I get in the "link" model through attr_accessor. The link model then executes the find_or_create method in that domain.

Now my solution is unsatisfactory: When the user clicks "Send link", do I put? Other = prompt in the URL, and then has a conditional code like this:

<% 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 Site", new_link_path(:other => "yes")%></span>
</p>

When the user makes a choice between two options in the form, it is displayed 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 %>

The problem is that if the user makes a mistake and does not perform some checks, the code "render: action => 'new" is executed, and all the information in the parameters is lost. Is there a way to save this information, or perhaps another way to do it altogether?

+3
source share
1 answer

I only have a vague understanding of your problem, but it looks like you want this:

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

Thus, the current value otherwill be resubmitted, and if the verification fails, it will still be available.

+6
source

Source: https://habr.com/ru/post/1757926/


All Articles