我有一個頁面,用戶(使用Devise)通過複選框設置多個首選項,然後設置預定義數據的單選按鈕。到目前爲止,我有用戶能夠更新has_and_belongs_to_many關聯,但我無法讓我的belongs_to工作。無法更新Rails中的用戶belongs_to關聯
此刻我得到這個錯誤與所示的參數如下:
PG::ForeignKeyViolation: ERROR: insert or update on table "users" violates foreign key constraint
{"utf8"=>"✓",
"_method"=>"patch",
"user"=>{"sport_ids"=>["4"], "goal_ids"=>["6"], "moment_id"=>"moment_id", "workout_id"=>"workout_id"},
"commit"=>"Save Changes",
"id"=>"1"}
似乎很清楚,我不是在傳遞一個ID號通過,但我不知道如何解決它。當我沒有收到錯誤時,什麼也沒有發生。
這裏是我的文件
型號/ user.rb
class User < ApplicationRecord
belongs_to :city
belongs_to :workout
belongs_to :fitness_level
belongs_to :moment
has_and_belongs_to_many :sports
has_and_belongs_to_many :goals
has_and_belongs_to_many :gyms
end
控制器/ users_controller.rb
class UsersController < ApplicationController
...
def edit
@user = User.find(params[:id])
@auth = current_user.id
# To make sure you can't edit someone elses profile
if @auth != @user.id
redirect_to @user
end
@sports = Sport.all.order(name: :asc)
@goals = Goal.all.order(name: :asc)
@workouts = Workout.all.order(:name)
@moments = Moment.all
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to @user
else
render 'edit'
end
end
def show
@user = User.find(params[:id])
end
private
def user_params
params.require(:user).permit(sport_ids: [], goal_ids: [])
params.require(:user).permit(:workout_id, :moment_id)
end
end
用戶/ edit.html.erb
<%= simple_form_for @user do |f| %>
# The following two work
<% @sports.each do |sport| %>
<%= check_box_tag "user[sport_ids][]", sport.id, form.object.sports.include?(sport) %>
<%= sport.name %>
<% end %>
<% @goals.each do |goal| %>
<%= check_box_tag "user[goal_ids][]", goal.id, form.object.goal.include?(goal) %>
<%= sport.name %>
<% end %>
# the below doesn't work
<% @moments.each do |moment| %>
<%= radio_button_tag 'user[moment_id]', :moment_id %>
<h4><%= moment.name %></h4>
<% end %> <!-- end moments-->
<% @workouts.each do |workout| %>
<%= radio_button_tag 'user[workout_id]', :workout_id %>
<% end %> <!-- end workouts-->
<% end %> <! -- end form -->
我有一些重要的造型機智h使用標籤的表格,以便需要留下。
編輯:在架構添加用戶表
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.jsonb "settings", default: {}, null: false
t.string "first_name"
t.string "last_name"
t.date "date_of_birth"
t.integer "city_id"
t.text "bio"
t.integer "workout_id"
t.integer "fitness_level_id"
t.integer "moment_id"
t.index ["city_id"], name: "index_users_on_city_id", using: :btree
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["fitness_level_id"], name: "index_users_on_fitness_level_id", using: :btree
t.index ["moment_id"], name: "index_users_on_moment_id", using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
t.index ["settings"], name: "index_users_on_settings", using: :gin
t.index ["workout_id"], name: "index_users_on_workout_id", using: :btree
end
你正在使用哪個數據庫? PostgreSQL? – Kavincat
嘿,我使用的是postgres –
您是否嘗試過使用'<%= radio_button_tag'用戶[moment_id] []',moment.id%>'或'<%= radio_button_tag'用戶[moment_id]',moment.id%>' ?在'users/edit.html.erb'中有 –