我有一個關聯我的rails應用程序中的兩個模型的問題:用戶&配置文件。新用戶註冊後應創建個人用戶配置文件。註冊用戶後,將數據保存到實際配置文件模型中不成功。我無法讓它工作。請在下面找到詳細的說明。將數據保存到配置文件模型不起作用
這裏是我的設置:
我使用Rails的4.0.0.rc2和Ruby 2.0.0p195。
兩款機型都喜歡此相關:
profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
user.rb
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
before_create :build_profile
,因爲我用我創建了一個registrationscontroller
改變after_sign_up_path的devise gem
:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
new_user_profile_path(:user_id => @user)
end
end
每當我註冊一個新用戶時,實際註冊都可以正常工作,例如,用戶隨後被導向http://localhost:3000/users/42/profile/new
。然而,當我再輸入數據到配置文件表單字段,然後點擊提交,我得到以下錯誤:
No route matches [POST] "https://stackoverflow.com/users/profile"
儘管人們可以期待一個路由錯誤,你會發現一個不同的錯誤查看實際域時:
http://localhost:3000/users//profile
如果你還是想看看我的routes.rb
請做(相關節選):
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
get 'signup', :to => "devise/registrations#new", as: :signup
get 'login', :to => "devise/sessions#new", as: :login
get 'logout', :to => "devise/sessions#destroy", as: :logout
end
resources :users do
resource :profile
end
然而,如上所述,我不重複盟友有路由問題。看起來好像我有一個問題,當前user_id沒有正確顯示在域中,這可能與我的實際配置文件形式或配置文件控制器上的新操作有關。
我開始我的個人資料表格上new.html.erb這樣的:
<%= form_for @profile, url: user_profile_path(@user) do |f| %>
我profiles_controller.rb
看起來是這樣的:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
# GET /profiles
# GET /profiles.json
def index
@profiles = Profile.all
end
# GET /profiles/1
# GET /profiles/1.json
def show
end
# GET /profiles/new
def new
@profile = Profile.new
end
# GET /profiles/1/edit
def edit
end
# POST /profiles
# POST /profiles.json
def create
@profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
format.json { render action: 'show', status: :created, location: @profile }
else
format.html { render action: 'new' }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /profiles/1
# PATCH/PUT /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_profile
#@profile = Profile.find(params[:id])
@profile = current_user.profile
end
# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params.require(:profile).permit(:photo, :address, :zip, :city, :state, :country, :telephone, :experience, :levels, :ages, :travel, :teachinglocation, :onlineteaching, :quotation, :aboutme, :subjects, :specialties, :lessondetails, :equipment)
end
end
我該怎麼辦錯了嗎?我怎樣才能確保新註冊的用戶可以正確保存他的個人資料數據?
如果你能幫我解決問題,那就太好了。