将数据保存到配置文件模型不起作用

问题描述:

我有一个关联我的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://*.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 

我该怎么办错了吗?我怎样才能确保新注册的用户可以正确保存他的个人资料数据?

如果你能帮我解决问题,那就太好了。

看来配置文件控制器没有配置正确的URL - 可以通过缺少的用户ID信息来看到。

您可以通过在命令行中运行该命令rake routes看到所有当前定义的路径。

我在REST风格的设计和Rails是初学者,所以不要考虑下面的专家建议。

试一试更改个人资料中的redirect_to的位置创建方法:

# POST /profiles 
# POST /profiles.json 
def create 
... 
    format.html { redirect_to user_profile_path(@profile.user, @profile), notice: 'Profile was successfully created.' } 
... 

如果这个作品中,路径也应该在更新更新和删除的方法。以及format.json部分。

有关此主题查看更多信息:
2.9创建路径和URL从对象
http://guides.rubyonrails.org/routing.html