javascriptruby-on-railsajaxrenderpartial

Rails missing partial *.js.erb


I'm trying to inform user about errors without reloading the whole page, but I get an error "AnswersController#create is missing a template for this request format and variant"

create.js.erb

$('.answer-errors').html("<%= render 'shared/errors', resource: @answer %>");

AnswersController#create

def create
  @answer = question.answers.build(answer_params)

  if @answer.save
    redirect_to question, notice: t('.success')
  end
end

app/views/shared/_errors.html.slim exist.

app/views/answers/_form.html.slim - this is a form for new answer

= form_with model: [question, answer], class: 'form-answer hide' do |form|
 = form.label :body
 = form.text_area :body
 = form.submit 'Save'

console log. I see here "Processing by AnswersController#create as HTML" but should it be "..as js"?

Started POST "/questions/3/answers" for 127.0.0.1 at 2021-08-23     21:19:08 +0300
Processing by AnswersController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "answer"=>    {"body"=>""}, "commit"=>"Save", "question_id"=>"3"}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Question Load (0.7ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ app/controllers/answers_controller.rb:27:in `question'
Completed 406 Not Acceptable in 8ms (ActiveRecord: 1.3ms | Allocations: 4424)

ActionController::UnknownFormat (AnswersController#create is missing a template for this request format and variant.

request.formats: ["text/html"]
request.variant: []):

Where can be a problem?


Solution

  • Many thanks to @max for link to guide.

    form_with model: [question, answer], class: 'form-answer hide', local: false do..
    

    This work fine, but I still don't get why remote: true not the same as local: false. That's counterintuitively to me and any hints will be pleasant