2016-08-25 104 views
0

請幫幫我。我一直在扯掉我的頭髮,試圖弄清楚我覺得應該是一件簡單的事情。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 [email protected]["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

我相信這個問題是由您使用「配置文件」,當你的協會其實是一個has_one引起的。

嘗試改變:

  • profiles_attributesprofile_attributes在travellers_controller#update_traveler_params
  • :profiles:profile在_profilefields.html.erb
+0

我不能相信這是解決方案。非常感謝。這讓我傷了幾個小時。 –

+0

沒問題。如果能解決您的問題,您能否將我的答案標記爲已接受? :] – pdoherty926