我在我的 rails 应用程序中关联两个模型时遇到问题:用户和配置文件。新用户注册后应创建个人用户配置文件。注册用户后,将数据保存到实际的配置文件模型中是不成功的。我无法让它工作。请在下面找到详细说明。
这是我的设置:
我使用 Rails 4.0.0.rc2 和 ruby 2.0.0p195。
两个模型的关联如下:
配置文件.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
用户.rb
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
before_create :build_profile
当我使用时,devise gem
我创建了一个registrationscontroller
来更改 after_sign_up_path:
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] "/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
我做错了什么?如何正确确保新注册的用户可以正确保存他的个人资料数据?
如果你能帮助我,那就太好了。