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 %>
traveler_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
とスキーマ。プロファイルの外部キーが正しく設定されていますか?
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
に
:profile
へ:profiles
をupdate_traveler_params。どうもありがとうございます。これは何時間も私を殺していた。 –問題ありません。あなたの問題を解決したら、私の答えに合格とマークしてください。 :] – pdoherty926