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

时间:2013-10-20 03:03:42

标签: ruby-on-rails devise ruby-on-rails-4

我在导轨应用程序中关联两个模型时遇到问题:用户&配置文件。在新用户注册后,应创建单个用户配置文件。注册用户后,将数据保存到实际的配置文件模型中是不成功的。我无法让它发挥作用。请在下面找到详细说明。

以下是我的设置:

我使用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 

当我使用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

我做错了什么?如何正确确保新注册的用户可以正确保存其个人资料数据?

如果你可以帮助我的话会很棒。

1 个答案:

答案 0 :(得分:0)

配置文件控制器似乎没有配置正确的URL - 缺少的用户ID信息可以看出。

您可以通过从命令行运行命令rake routes来查看所有当前定义的路径。

我是RESTful设计和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