0

I'm trying to create a default and disabled 'please select' option for f.select in simpleform and have selecting an option required. Here's the code:

<div class="form-group mb-3">
            <%= f.label :buyer_type, "Which best describes you: " %>
            <%= f.select :buyer_type, ['Please Select','Option 1', 'Option 2', 'Option 3'], {}, { class: "form-control" } %>
          </div>

I've tried multiple fixes, ranging from prompt:, required:, disabled="disabled">Category</option, include_blank: and others, but I keep getting a syntax error: "ActionView::SyntaxErrorInTemplate in Users::RegistrationsController#new"

So I've looked in the registrations_controller.rb file, which has the following code:

# frozen_string_literal: true

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]
  before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  def new
    params[:role] ||= 'buyer'
    build_resource
    yield resource if block_given?
    render "new_#{params[:role]}"
  end

  def unconfirmed_registration
    flash.delete(:notice)
  end

  #
  # def after_sign_up_path_for(resource)
  #   # '/an/example/path' # Or :prefix_to_your_route
  #   new_user_session_path(resource)
  # end
  # POST /resource
  # def create
  #   super
  # end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  protected
  # Build a devise resource passing in the session. Useful to move
  # temporary session data to the newly created user.
  def build_resource(hash = {})
    self.resource = resource_class.new_with_session(hash, session)
  end

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:buyer_type, :first_name, :last_name, :registered_as])
  end

  # If you have extra params to permit, append them to the sanitizer.
  def configure_account_update_params
    devise_parameter_sanitizer.permit(:account_update, keys: [:buyer_type, :first_name, :last_name, :quality_inspection, :solicitor, :finance, :business_name, :bio, :abn, :registration, :wsite])
  end

  # The path used after sign up.
  def after_sign_up_path_for(resource)
    '/welcome'
  end

  #
  def update_resource(resource, params)
    if params[:password].present?
      resource.update_with_password(params)
    else
      resource.update_without_password(params)
    end
  end

  #
  def after_update_path_for(resource)
    edit_user_registration_path
  end

  def after_inactive_sign_up_path_for(resource)
    unconfirmed_registration_path
  end
end

So I believe it needs some sort of validation here, or perhaps somewhere else. I've tried including a validation for buyer_type in the User.rb, relating to my previous post linked to this one: Creating Required Fields for Website Forms (Simpleform) I've looked at adding a validation for f.select using validates :buyer_type

I can't tell where I'm going wrong! Any help would be greatly appreciated!

LJ-03
  • 49
  • 9
  • You could debug by comment code in the `new` action, to see which line breaks, start from only keep the first line and comment rest three line. – eux Feb 04 '21 at 01:19
  • Which sections are you referring to, specifically? Here?: ` def new params[:role] ||= 'buyer' build_resource yield resource if block_given? render "new_#{params[:role]}" end` – LJ-03 Feb 04 '21 at 01:31
  • Yes, as there's a syntax error: "ActionView::SyntaxErrorInTemplate in Users::RegistrationsController#new". – eux Feb 04 '21 at 01:33
  • So it seems as though 'new params' is working as it should be. The only other place I could find "#new" is "#new_user_session_path(resource)" which I uncommented and got the following error: "undefined local variable or method `resource' for Users::RegistrationsController:Class Did you mean? rescue". I definitely don't think it should mean rescue, so I'm not sure what's what here – LJ-03 Feb 04 '21 at 02:16
  • Or could it be an issue with `def new`? – LJ-03 Feb 04 '21 at 02:16
  • The `ActionView::SyntaxErrorInTemplate` should be in `Users::RegistrationsController#new" based on the error at first. Which line the error occur from the error log? Or could you paste the full error log? – eux Feb 04 '21 at 02:50
  • From the terminal: app/views/users/registrations/new_buyer.html.erb:53: syntax error, unexpected ',', expecting => app/views/users/registrations/new_buyer.html.erb:53: syntax error, unexpected ',', expecting => app/views/users/registrations/new_buyer.html.erb:53: syntax error, unexpected ',', expecting => app/views/users/registrations/new_buyer.html.erb:53: syntax error, unexpected ',', expecting => app/controllers/users/registrations_controller.rb:12:in `new' – LJ-03 Feb 04 '21 at 03:11
  • So the error should be at `app/views/users/registrations/new_buyer.html.erb:53` based on the error log. – eux Feb 04 '21 at 04:33
  • Yeah that was where I was trying to add the edit to `f.select`. Does that mean that there needs to be a validation or some other change to another file, possibly in the models? – LJ-03 Feb 04 '21 at 04:51
  • You should fix that line of code at `app/views/users/registrations/new_buyer.html.erb:53` according the error log, it's not related to other files. – eux Feb 04 '21 at 05:51

0 Answers0