Rails更新不保存值

时间:2016-08-25 23:49:24

标签: ruby-on-rails

请帮帮我。我一直在试图弄清楚我觉得应该是一件简单的事情。

我的更新功能“成功”但实际上并没有保存新值。控制台日志不会抛出错误,但它会说“未允许的参数:配置文件”

edit.html.erb

<div class="col-md-7 col-md-offset-3 main">
  <% provide(:title, "Edit user")%>
  <center><h1>Update your profile</h1></center>
  <%= form_for(@person) do |f| %>
    <%= render 'shared/error_messages' %>
    <div class="col-md-12">
      <%= render 'layouts/profilefields', f: f %>
      <%= f.submit "Save Changes", class: "btn btn-large btn-primary" %>
    </div>
  <% end %>
</div>

_profilefields.html.erb

<%= f.fields_for :profiles do |prf|%>
  <!--
  <% if !@profileInfo["avatar"].blank? %>
    <%= image_tag @contactInfo.avatar_url(:medium).to_s, :class=>"profilePhoto" %>
  <% end %>
  <div class="photoPreview">
    <i class="fa fa-upload photoUpload"></i>
    <p id="uploadClick">Click to Upload</p>
  </div>

  <%= prf.file_field :avatar, accept: 'image/png,image/gif,image/jpeg, image/jpg', id: 'uploadAvatar' %>
  <p class="deletePhoto">Delete</p>
  -->

  <%= prf.label :about %>
  <%= prf.text_field :about, :class => "form-control" %>
  <%= prf.label :why %>
  <%= prf.text_field :why, :class => "form-control" %>
  <%= prf.label :goals %>
  <%= prf.text_field :goals, :class => "form-control" %>


  <%= prf.hidden_field :traveler_id, value: current_traveler.id %>
<% end %>

travelers_controller.rb

class TravelersController < ApplicationController
  def edit
    @person = Traveler.find(params[:id])
    @profileInfo = Profile.find_or_initialize_by(traveler_id: params[:id])
    #@profileInfo[:email] = current_traveler.email 

    #This builds the form
    @person.build_profile(@profileInfo.attributes)
  end

  def show
  end

  def update
    @trav = Traveler.find(params[:id])
    #prof = Profile.find_or_create_by(traveler_id: current_traveler.id)
    if @trav.update(update_traveler_params) 
      flash[:success] = "Profile updated"
      redirect_to feed_path
    else # Failed. Re-render the page as unsucessful
      render :edit
    end
  end

  private 
    def update_traveler_params
      params.require(:traveler).permit(:id, profiles_attributes: [:id, :first_name, :last_name, :about, :why, :goals,
          :avatar, :traveler_id])
    end
end

模型

class Profile < ApplicationRecord
    belongs_to :traveler, foreign_key: "traveler_id"
end

class Traveler < ApplicationRecord
  # Include default devise modules. Others available are:
  # , :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable

  has_one :profile
  accepts_nested_attributes_for :profile, update_only: true, allow_destroy: true
end

架构。配置文件foreignkey是否设置正确?

ActiveRecord::Schema.define(version: 20160825224710) do

  create_table "profiles", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.text     "about",       limit: 65535
    t.text     "why",         limit: 65535
    t.text     "goals",       limit: 65535
    t.string   "avatar"
    t.integer  "traveler_id"
    t.datetime "created_at",                null: false
    t.datetime "updated_at",                null: false
    t.index ["traveler_id"], name: "index_profiles_on_traveler_id", using: :btree
  end

  create_table "travelers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    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.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
  end

end

1 个答案:

答案 0 :(得分:2)

我认为问题是由您使用“个人资料”造成的,当您的关联实际上是has_one时。

尝试更改:

    travellers_controller中的
  • profiles_attributesprofile_attributes#update_traveler_params
  • 在_profilefields.html.erb
  • :profiles:profile