2016-08-25 7 views
0

Bitte helfen Sie mir. Ich habe mir die Haare ausgerissen, um herauszufinden, was ich für eine einfache Sache halten sollte.Schienen aktualisieren nicht speichern

Meine Update-Funktion "ist erfolgreich", speichert aber nicht die neuen Werte. Das Konsolenprotokoll wirft keinen Fehler, aber es sagt "Unzulässige Parameter: Profile"

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 %> 

travels_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 

und die Modelle

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 

und das Schema. Ist der Profil-Fremdschlüssel korrekt eingerichtet?

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 

Antwort

1

Ich glaube, das Problem durch die Nutzung von „Profilen“ verursacht wird, wenn der Verein tatsächlich ein has_one ist. Ändern

Versuchen:

  • profiles_attributes zu profile_attributes in travellers_controller # update_traveler_params
  • :profiles-:profile in _profilefields.html.erb
+0

Ich kann die Lösung nicht glauben, dass war. Ich danke dir sehr. Das hat mich stundenlang umgebracht. –

+0

Kein Problem. Könnten Sie bitte meine Antwort als akzeptiert markieren, wenn sie Ihr Problem gelöst hat? :] – pdoherty926

Verwandte Themen