2017-04-04 53 views
1

我有一個頁面,用戶(使用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 
+0

你正在使用哪個數據庫? PostgreSQL? – Kavincat

+0

嘿,我使用的是postgres –

+0

您是否嘗試過使用'<%= radio_button_tag'用戶[moment_id] []',moment.id%>'或'<%= radio_button_tag'用戶[moment_id]',moment.id%>' ?在'users/edit.html.erb'中有 –

回答

1

Im相當肯定該行中的問題:

<%= radio_button_tag 'user[moment_id]', :moment_id %> 

你不從,以controller`s更新視圖通過moment_id和workout_id行動。

嘗試將其更改爲:

<% @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--> 

而且在同一行你爲什麼不通行許可PARAMS?像這樣:

def user_params 
    params.require(:user).permit(:moment_id, :workout_id, sport_ids: [], goal_ids: []) 
    end 
+0

當在一行中傳遞參數時,我得到這個:語法錯誤,意外的','期待=> [],goal_ids:[],:moment_id,:workout_id)^ –

+0

但似乎適用於兩條線?它現在正在更新,但每次我回到我的編輯頁面時,單選按鈕沒有被選中,使得它看起來沒有發生在用戶身上。任何想法爲什麼這是? (也非常感謝你的時間,謝謝) –

+0

@SamPrice是的我的壞。這是正確的'params.require(:user).permit(:moment_id,:workout_id,sport_ids:[],goal_ids:[])' –

相關問題