0
votes

I have some url

  get '/vacancies/:id(/:mode)', to: 'vacancies#show', as: 'vacancy', constraints: { mode: /(resume|file)/}

inside show view user can fill in the form and send request to the server. If User sent not valid params I should redirect him back, show validation error and also I should keep filled in params.

How can I implement this stuff?

upd1

Typical rails solution for this is something like render :new or render :edit, but in my case I can't do render :show because is rendering view for '/vacancies/:id route not for '/vacancies/:id(/:mode).

Also I tried do redirect_to :back and this works fine except one thing: params are not passed through request.

I guess there is solution inside response object, but didn't find yet

upd2

class VacanciesController < ApplicationController
  # vacancies/:id - is just show view
  # vacancies/:id/resume - is show view with form
  # vacancirs/:id/file - is show view with form for sending pdf|doc|etc
  def show
    @vacancy = Vacancy.find(params[:id])
  end

  def create
    if Vacancy::SaveResume.new(resume_params).call.valid?
      redirect_to vacancies_path
    else
      # didn't know what to do there
    end
  end
end
1
This is very basic Rails stuff. I suggest giving this guide a read and then posting a more detailed question if you're still stuck. - Thilo
@Thilo I just updated post - DmitrySharikov
you'd better use ajax in this particular case. - Ilya

1 Answers

0
votes

In your controller, in order to maintain those params, you would re-render that same view template instead of redirecting. This happens a lot with the new and create action, but the same priniciple applies anywhere, such as displaying a form in a show action.

class PostsController < ApplicationController
  def new
    @post = Post.new
    render :new
  end

  def create
    @post = Post.new(params[:post])
    if @post.save
      redirect_to :show
    else
      render :new # re-render another template
    end
  end
end