Rails: использование reCaptcha в форме для модели с параметрами, переданными в - PullRequest
0 голосов
/ 26 января 2019

Я пытаюсь добавить подтверждение reCaptcha в форму в моем приложении Rails.Он отлично работает, когда в других случаях я использую его, но с определенной вложенной моделью, в которой передан параметр, я получаю сообщение об ошибке, в котором говорится, что в нем отсутствует параметр (только в случае сбоя reCaptcha).

Я получаю следующую ошибку:

ActionController :: ParameterMissing в QuestionsController # создать параметр отсутствует или значение пустое: Ques_num

Кажется, что параметры Ques_num действительнохотя, как это можно увидеть на странице с ошибкой:

Parameters:

{"utf8"=>"✓","authenticity_token"=>"4NTPiMB/iVDVsaIhs4wEfkK4jNPtLeGWq8QI3uGA+IoS9pXjnKQk+YCRo3ZPsFGmq1JtZEzf/5RAwfNXNLPIdQ==
", "question"=>{"content"=>"<p>test</p>\r\n", "ques_num"=>"7", "coin_id"=>"1"},
 "g-recaptcha-response"=>"",
 "commit"=>"Submit",
 "coin_id"=>1}

Я предполагаю, что мне нужно передать параметр: Ques_num другим способом, однако я не уверен, как это сделать.о том, как это сделать.

В настоящее время он настроен следующим образом:

questions_controller.rb

class QuestionsController < ApplicationController
  load_and_authorize_resource
  before_action :find_coin, except: [:editor_images,:geteditor_images,:display_term_keyplayer]
  before_action :find_ques_num, only: [:index]
  before_action :authenticate_user!, except: [:show, :display_term_keyplayer, :terms, :keyplayers]
  before_action :find_user
  before_action :ensure_url_params, only:[:new]
  skip_before_action :verify_authenticity_token, :only => [:editor_images]

  ...
  def create
    @question = current_user.questions.build(question_params)
    @question.coin = @coin
    if verify_recaptcha(model: @question) && @question.save
      Notification.create(recipient: @coin.moderator, actor: current_user, action: "submitted", notifiable: @question)
      flash[:notice] = "Your submission has been accepted and will be reviewed by a moderator."
      redirect_to coin_path(@coin)
    else
      flash[:notice] = "reCaptcha verification unsuccessful. Please try again."
      render 'new'
    end
  end
  ...

  private 
    ...
    def question_params
      params.require(:question).permit(:content, :ques_num, :coin_id, :accepted, :term, :description, :caption, :image, :image_caption, :open_topic, :slug, :flagged)
    end
end

app / views /questions / new.html.erb

<% provide(:title, "Submit an Answer") %>
<div class="form-container">
  <div class="form-container__form-section">
    <% @ques_num = params.fetch(:ques_num).to_i %>
    <% if @ques_num == 1 %>
        <h3><center>General Overview</center></h3>
    <% elsif @ques_num == 2 %>
        <h3><center>Terminology</center></h3>
    ...
    <% else %>
      <h3>Submit Answers</h3>
    <% end %>
  </div>
  <%= render 'form' %>
</div>

_form.html.erb

<% provide(:title, "Submit a Link") %>
<%= simple_form_for @question, url: coin_questions_path(@coin) do |f| %>
  <div class="form-container__section">
    <% if @ques_num == 2 %>
      ... 
    <% elsif @ques_num == 3 %>
      ...
    <% elsif @ques_num == 5 %> 
    ...
    <% else %>          
      ...
    <% end %>

    <%= f.hidden_field :ques_num, value: @ques_num %>
    <%= f.hidden_field :coin_id, value: @coin.id %>

    <div class="recaptcha-form-group">
      <%= recaptcha_tags %>
    </div>

    <div class='form-container__horizontal-button-group'>
      <%= f.button :submit, 'Submit', class: 'primary-small' %>
      <%= link_to "Back", coin_path(@coin), class: "secondary-small" %>
    </div>
  </div>
<% end %>

Что я здесь не так делаю?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...