简体   繁体   中英

Creating profile page for devise user RoR

I am new to RoR and am struggling with what seems to be this basic project requirement.

I am using Devise for user authentication which works fine and upon registering a user account with Devise I am using a custom redirect in the registrations controller to a new page where the user can create their profile. After the registration I keep getting the following error:

"First argument in form cannot contain nil or be empty". Can someone point me in the right direction? Thanks in advance!

Code below:

Registrations Controller:

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    new_path(resource)
  end
end

Create New profile page:

<%= form_for @profile do |f| %>

<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>

<div class="actions">
<%= f.submit %>
</div>

<% end %>

Profile controller:

class ProfileController < ApplicationController

def new
 @profiles = current_user.build_profile
end

def create
 @profiles = current_user.build_profile(params[:profile].permit( :name))
end 

end

Profile model:

class Profile < ActiveRecord::Base

belongs_to :user

end

User model:

class User < ActiveRecord::Base

devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable

validates :name, presence: true

has_one :profile

end

DB Schema file:

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

  create_table "profiles", force: true do |t|
    t.integer  "user_id"
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "location"
    t.string   "email",                  default: "", null: false
    t.string   "phone"
    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.datetime "created_at"
    t.datetime "updated_at"
  end

In your ProfileController, you assign @profiles = current_user.build_profile , but in your form, you reference form_for @profile do |f| . Note the difference in pluralization. Change it to @profile in the controller, and you should be good to go...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM