种子收集数据不起作用

时间:2018-06-28 05:00:41

标签: ruby-on-rails ruby database seed

我已将种子数据添加到class_details表并进行了迁移。在我的user_classes表中,我需要获取用户选择的class_detail的对应ID。但是我无法将class_detail ID保存在user_class表中。 user_class表取决于用户表。谁能帮助我!预先感谢。

seed.rb

ClassDetail.create!([
  {standard_name: "Class 1 to 5" , class_category: "PRIMARY"},
  {standard_name: "Class 6 to 8" , class_category: "MIDDLE"},
  {standard_name: "Class 9 to 10" , class_category: "SECONDARY"},
  {standard_name: "Class 11 to 12" , class_category: "HIGHER SECONDARY"}
  ]) 

user_class.rb

class UserClass < ApplicationRecord
  belongs_to :user
  belongs_to :class_detail
  accepts_nested_attributes_for :class_detail, update_only: true

  validates_presence_of :user_id
end

class_detail.rb

class ClassDetail < ApplicationRecord
  has_many :user_class
  has_many :users, through: :user_class
end

user.rb

has_one :user_class, dependent: :destroy
has_many :class_details, through: :user_classes

控制器

def update_profile
    if @user.update(user_params)
      redirect_to profile_index_path, notice: 'Profile was successfully updated.'
    else
      render :index
    end
  end
private

  def set_user
    @user = User.find(current_user.id)
    @user.user_class || @user.build_user_class
  end

  def user_params
    params.require(:user).permit(:name,user_class_attributes: %i[id standard_name class_category class_detail_id])
  end
end

查看代码

 <%= form_for(@user, url: {action: 'update_profile'}, html: {class: 'form-horizontal'}) do |f| %>
<%= f.fields_for :user_class, @user.user_class do |c| %>
<div class="card card-primary">
  <div class="card-header">
    <h3 class="card-title">
      <%= t('.education') %>
    </h3>
  </div>
  <div class="card-body">
    <div class="form-group">
      <label for="example-text-input">
        Class Name
      </label>
      <div class="col-sm-10">
        <%= c.select :class_detail_id, ClassDetail.all.collect { |p| [ p.standard_name, p.id ] }, {}, {class: 'form-control'} %>
      </div>
    </div>
  </div>
</div>
<% end %>
<div class="card-footer">
  <%= f.submit 'Save Changes', class: 'btn btn-primary' %>
  &nbsp;&nbsp;
  <%= link_to 'Back', root_path, class: 'btn btn-secondary' %>
</div>
<% end %>

终端日志

Started PATCH "/profile/update_profile" for 127.0.0.1 at 2018-06-28 10:27:56 +0530
Processing by ProfileController#update_profile as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"qFvn2SHK2LO3pxg1YwJjG02UPHQqWhJGxUt2DIOfdwm+eHnNxzJRilwYgjaibXwIL7ILgkUxM2cIz3p42JSoxA==", "user"=>{"name"=>"admin", "email"=>"admin@gmail.com", "address_attributes"=>{"area"=>"", "city"=>"", "state"=>"", "country"=>"", "postcode"=>"", "id"=>"1"}, "education_attributes"=>{"qualification_type"=>"", "name"=>"", "issue_institute"=>"", "education_status"=>"", "id"=>"1"}, "user_class"=>{"class_detail_id"=>"3"}, "fee_attributes"=>{"fee_hour"=>"", "fee_month"=>"", "id"=>"1"}}, "commit"=>"Save Changes"}
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.2ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Address Load (0.2ms)  SELECT  `addresses`.* FROM `addresses` WHERE `addresses`.`user_id` = 1 LIMIT 1
  Fee Load (0.2ms)  SELECT  `fees`.* FROM `fees` WHERE `fees`.`user_id` = 1 LIMIT 1
  Education Load (0.3ms)  SELECT  `educations`.* FROM `educations` WHERE `educations`.`user_id` = 1 LIMIT 1
  UserClass Load (0.2ms)  SELECT  `user_classes`.* FROM `user_classes` WHERE `user_classes`.`user_id` = 1 LIMIT 1
Unpermitted parameters: :email, :user_class
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
Redirected to http://localhost:3000/profile
Completed 302 Found in 15ms (ActiveRecord: 1.8ms)


Started GET "/profile" for 127.0.0.1 at 2018-06-28 10:27:56 +0530
Processing by ProfileController#index as HTML
  User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.9ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Address Load (0.9ms)  SELECT  `addresses`.* FROM `addresses` WHERE `addresses`.`user_id` = 1 LIMIT 1
  Fee Load (0.4ms)  SELECT  `fees`.* FROM `fees` WHERE `fees`.`user_id` = 1 LIMIT 1
  Education Load (0.3ms)  SELECT  `educations`.* FROM `educations` WHERE `educations`.`user_id` = 1 LIMIT 1
  UserClass Load (0.3ms)  SELECT  `user_classes`.* FROM `user_classes` WHERE `user_classes`.`user_id` = 1 LIMIT 1
  Rendering profile/index.html.erb within layouts/application
  Rendered profile/_profile_card.html.erb (1.7ms)
  Rendered profile/_profile_tabs.html.erb (0.7ms)
  ClassDetail Load (0.3ms)  SELECT `class_details`.* FROM `class_details`
  Rendered profile/_profile_detail.html.erb (6.4ms)
  Rendered profile/index.html.erb within layouts/application (11.5ms)
  Rendered layouts/_web_font.html.erb (0.3ms)
  Rendered layouts/_header.html.erb (0.7ms)
  Rendered layouts/_left_menu.html.erb (1.6ms)
  Rendered layouts/_alerts.html.erb (0.2ms)
  Rendered layouts/_right_menu.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 50ms (Views: 37.0ms | ActiveRecord: 3.5ms)


Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-06-28 10:27:56 +0530
ConversationChannel stopped streaming from conversations-1
Started GET "/cable" for 127.0.0.1 at 2018-06-28 10:27:56 +0530
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-06-28 10:27:56 +0530
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Registered connection (Z2lkOi8vdGVhY2gtbWUvVXNlci8x)
ConversationChannel is transmitting the subscription confirmation
ConversationChannel is streaming from conversations-1

0 个答案:

没有答案