ruby on rails - Prepopulating form with data after API sends error in JSON -
i have frontend rails app requesting rails api. when user wants create account, submitting account form on front-end app :
<h1>create account</h1> <form action="http://localhost:3000/accounts" method="post"> <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden"> <div class="form-group"> <%= label_tag 'account[name]', "name" %> <input type="text" name="account[name]" required> </div> <div class="form-group"> <%= label_tag "account[contact_mail]", "e-mail" %> <input type="text" name= "account[contact_mail]" required> </div> <div class="form-group"> <%= label_tag "account[contact_tel]", "telephone" %> <input type="text" name= "account[contact_tel]" required> </div> <div class="form-group"> <%= label_tag "account[iban]", "iban" %> <input type="text" name= "account[iban]"> </div> <div class="form-group"> <%= label_tag "account[bic]", "bic" %> <input type="text" name= "account[bic]"> </div> <input type="submit"> <% if @errors %> <ul class="list-unstyled"> <%@errors.each |error|%> <li class="has-error"><%=error%></li> <% end -%> </ul> <% end -%> </form> on submit method create front-end app account controller called. create method front-end app calling create method on rails api responsible updating database , rendering json front end app.
1) accountcontroller#create front-end app :
def create
# post sur api create account @response = httparty.post(env['api_address']+'api/v1/accounts', :body => { :name => params[:account][:name], :contact_mail => params[:account][:contact_mail], :contact_tel => params[:account][:contact_tel], :legal_status => params[:account][:legal_status], :iban => params[:account][:iban], :bic => params[:account][:bic] }.to_json, :headers => { 'x-user-email' => session[:user_email], 'x-user-token'=> session[:user_token], 'content-type' => 'application/json' } ) # erreur mauvais auth token if @response["error"] == "you need sign in or sign before continuing." redirect_to unauthorized_path # erreur de validation elsif @response["errors"] @errors = @response["errors"] flash[:alert] = "heello" render :new else account_id = @response["account"]["id"] redirect_to account_path(account_id) end end 2) accountcontroller#create api :
def create @account = account.new(account_params.merge(admin: current_user)) authorize @account if @account.save render :show else render_error end end
render error method render errors in json format :
def render_error render json: { errors: @account.errors.full_messages }, status: :unprocessable_entity end if there validations errors when submitting api, display them on front end app next form :
<% if @errors %> <ul class="list-unstyled"> <%@errors.each |error|%> <li class="has-error"><%=error%></li> <% end -%> </ul> my problem wish prepopulate form data user submitted better user experience.
i know there posts how prepopulate forms rails in case of validation errors feel did not address problem "double app pattern" , front-end app not having models (all models / db interactions dealt in api)
how can prepopulate form user's input after receive error json api ?
in form have, add value attributes input fields populate irb instance variables <input type="text" name="account[name]" value="<%= @account_name %>" required>
then in accountcontroller#create, set instance variables based on the params received when want display invalid data.
```
elsif @response["errors"] @errors = @response["errors"] #new code @account_name = params[:account][:name] #set each variable, method or class cleaner style flash[:alert] = "heello" render :new ```
when form renders these variables uninitialize/nil there no values in form boxes, , can explicitly set them in controller when validation fails want maintain data in form.
this overly-manual way accomplish maintaining form state, , suggest if want reuse pattern in app take @ using form_for. post might give insight doing plain ruby objects instead of model objects. is possible create form simple class
Comments
Post a Comment