Rails没有从单选按钮将数据获取到PostgreSQL



我正在构建一个应用程序,用户必须在其中输入电子邮件、密码和性别才能创建配置文件。我们只使用了电子邮件和密码,并决定稍后添加性别,但用户的个人资料中没有他们的性别。

我使用Devise登录,这就是为什么我没有";新的";在我的用户控制器中,但可能需要更改?

用户.rb

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many_attached :photos
has_one_attached :avatar
geocoded_by :location
after_validation :geocode, if: :will_save_change_to_location?
include PgSearch::Model
pg_search_scope :search_by_gender_location_and_breed,
against: [ :gender, :location, :breed ],
using: {
tsearch: { prefix: true }
}
end

new.html.erb

<div id="sign-up">
<div class="container w-25 p-3 text-center input-container">
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email,
required: true,
autofocus: true,
input_html: { autocomplete: "email" }%>
<%= f.input :password,
required: true,
hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: true,
input_html: { autocomplete: "new-password" } %>
<%= f.collection_radio_buttons :gender, [['male', 'male'] ,['female', 'female']],:first, :last %>
</div>
<%= f.button :submit, "Sign up", class:"sign_up_button" %>
<% end %>
<p>Or</p>
<%= render "devise/shared/links", class: "mb-3"  %>
</div>

用户控制器

class UsersController < ApplicationController
def index
if params[:query].present?
@users = User.search_by_gender_location_and_breed(params[:query])
@searched = true
else
@users = User.all
end
if params[:breed].present?
@users = @users.select { |n| n.breed == params[:breed] }
end
end
def show
@user = User.find(params[:id])
end
def like
@user = User.find(params[:id])
@chatroom = Chatroom.create(engager_id: current_user.id, receiver_id: params[:id])
redirect_to user_path
end
def dislike
@chatroom = Chatroom.find_by(engager_id: current_user.id, receiver_id: params[:id])
@chatroom.destroy
redirect_to user_path
end
def favorites
fave_chats = Chatroom.where(engager_id: current_user.id)
@favorites = []
fave_chats.each do |chat|
other_id = chat.receiver_id
@favorites << User.find(other_id)
end
end
def edit
@user = current_user
end
def update
@user = current_user
respond_to do |format|
format.html { redirect_to edit_user_registration_path }
if avatar_params.present?
@user.update(avatar_params)
format.text { render partial: 'devise/registrations/avatar_form', locals: { user: @user }, formats: [:html] }
elsif owner_params.present?
@user.update(owner_params)
format.text { render partial: 'devise/registrations/owner_info_form', locals: { user: @user }, formats: [:html] }
elsif photos_params.present?
@user.update(photos_params)
format.text { render partial: 'devise/registrations/carousel', locals: { user: @user }, formats: [:html] }
else
@user.update(user_params)
format.text { render partial: 'devise/registrations/cat_info_form', locals: { user: @user }, formats: [:html] }
end
end
end
def show
@user = User.find(params[:id])
end
private
def user_params
params.require(:user).permit(:name, :gender, :age, :breed, :description)
end
def avatar_params
params.require(:user).permit(:avatar)
end
def owner_params
params.require(:user).permit(:owner_name, :location, :owner_description)
end
def photos_params
params.require(:user).permit(photos: [])
end
end

info.html.erb

<%= form_for @user, html: { data: { edit_cat_target: 'form' } } do |f| %>
<p data-action="click->edit-cat#displayNameField">
<b>Name: <%= f.text_field :name, 'data-edit-cat-target': 'name', class: 'not-focused', 'data-action': 'blur->edit-cat#update' %></b>
</p>
<p data-action="click->edit-cat#displayAgeField">
<b>Age: <%= f.text_field :age, 'data-edit-cat-target': 'age', class: 'not-focused', 'data-action': 'blur->edit-cat#update' %></b>
</p>
<p>
<b>Gender: <%= @user.gender %></b>
</p>
<p data-action="click->edit-cat#displayBreedField">
<b>Breed: <%= select :user, :breed, ['Abyssinian', 'American Shorthair', 'British Shorthair', 'Bengal', 'Birman', 'Burmese', 'Cornish Rex', 'Devon Rex', 'Exotic Shorthair', 'Himalayan', 'Maine Coon', 'Norwegian Forest Cat', 'Ocicat', 'Oriental Shorthair', 'Persian', 'Ragdoll', 'Russian Blue', 'Scottish Fold', 'Siamese', 'Siberian', 'Sphynx', 'Tonkinese', 'other - but still fabulous'], { include_blank: true }, 'data-edit-cat-target': 'breed', 'data-action': 'blur->edit-cat#update' %></b>
</p>
<p data-action="click->edit-cat#displayDescriptionField">
<b>Description: <%= f.text_area :description, size: '40x10', 'data-edit-cat-target': 'description', class: 'not-focused', 'data-action': 'blur->edit-cat#update' %></b>
</p>
<% end %>

devise.en.yml

devise:
confirmations:
confirmed: "Your email address has been successfully confirmed."
send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes."
send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes."
failure:
already_authenticated: "You are already signed in."
inactive: "Your account is not activated yet."
invalid: "Invalid %{authentication_keys} or password."
locked: "Your account is locked."
last_attempt: "You have one more attempt before your account is locked."
not_found_in_database: "Invalid %{authentication_keys} or password."
timeout: "Your session expired. Please sign in again to continue."
unauthenticated: "You need to sign in or sign up before continuing."
unconfirmed: "You have to confirm your email address before continuing."
mailer:
confirmation_instructions:
subject: "Confirmation instructions"
reset_password_instructions:
subject: "Reset password instructions"
unlock_instructions:
subject: "Unlock instructions"
email_changed:
subject: "Email Changed"
password_change:
subject: "Password Changed"
omniauth_callbacks:
failure: "Could not authenticate you from %{kind} because "%{reason}"."
success: "Successfully authenticated from %{kind} account."
passwords:
no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided."
send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes."
send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
updated: "Your password has been changed successfully. You are now signed in."
updated_not_active: "Your password has been changed successfully."
registrations:
destroyed: "Bye! Your account has been successfully cancelled. We hope to see you again soon."
signed_up: "Welcome! You have signed up successfully."
signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated."
signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked."
signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account."
update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirmation link to confirm your new email address."
updated: "Your account has been updated successfully."
updated_but_not_signed_in: "Your account has been updated successfully, but since your password was changed, you need to sign in again."
sessions:
signed_in: "Signed in successfully."
signed_out: "Signed out successfully."
already_signed_out: "Signed out successfully."
unlocks:
send_instructions: "You will receive an email with instructions for how to unlock your account in a few minutes."
send_paranoid_instructions: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes."
unlocked: "Your account has been unlocked successfully. Please sign in to continue."
errors:
messages:
already_confirmed: "was already confirmed, please try signing in"
confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one"
expired: "has expired, please request a new one"
not_found: "not found"
not_locked: "was not locked"
not_saved:
one: "1 error prohibited this %{resource} from being saved:"
other: "%{count} errors prohibited this %{resource} from being saved:"

我还试图为我的用户添加性别验证,但因为这是一个输入字段,我找不到如何做到这一点,但我认为这与验证无关。

谢谢!Onur

我通过在我的设备中添加额外的参数来解决这个问题。基本上遵循了这里的步骤。

https://github.com/heartcombo/devise#strong-参数

谢谢大家!Onur

最新更新