2017-04-04 32 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:[])' –

相关问题