jeudi 29 mars 2018

How to retain data in input fields after a page reload Rails

I am building a job app with Rails. In my job 'new' page. I have a preview functionality where a user can click a 'preview button' to see how the job would look like when posted. On the preview page, the user can either post the job or click 'edit' to go back to the job new page.

The issue is that, when 'edit' is clicked the page reloads and all the data in the input fields get erased. I want to be able to preserve the data after the page reload so the user can edit the page without having to fill out forms from scratch. This is my controller action

def create
    @job = Job.new(job_params)
      if params[:previewButt] == "Preview"
        flash[:alert] = "This is a PREVIEW of your job posting".
        render :create
      elsif params[:createButt] == "Post Job"
        @job.save
        redirect_to root_path
      elsif params[:backButt] == "Make changes"
        render :new
      end
  end

In the 'preview' or 'create' view. I have this at the bottom of the page

<div class="col-md-9 text-center" style="margin-top: 110px;">
      <%= f.button :submit , name: "backButt", value: "Make changes" %>
      <%= f.button :submit , name: "createButt", value: "Post Job", class: "btn btn-success post-btn" %>
</div>

So ideally when the user clicked "Make changes" and the page reloads to render "new", the data in the form inputs should still be available.

Aucun commentaire:

Enregistrer un commentaire