配置设计宝石

时间:2016-02-25 17:41:33

标签: ruby-on-rails devise profile

已更新

我正在为用户创建配置文件并通过devise gem对其进行身份验证,并在默认的sqlite数据库中遇到问题并更改为mysql2,因此再次出现运行rake db:migrate

的错误

ERROR:

E:True

模型/ profiles.rb:

'fsftn@ubuntu:~/pls$ rake db:migrate
== 20160225174300 CreateProfiles: migrating===================================
-- create_table(:profiles)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'profiles' already exists: CREATE TABLE `profiles`     (`id` int(11) auto_increment PRIMARY KEY, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB/home/fsftn/pls/db/migrate/20160225174300_create_profiles.rb:4:in `change'
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'profiles' already exists: CREATE TABLE `profiles` (`id` int(11) auto_increment PRIMARY KEY, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB
/home/fsftn/pls/db/migrate/20160225174300_create_profiles.rb:4:in `change'
Mysql2::Error: Table 'profiles' already exists
/home/fsftn/pls/db/migrate/20160225174300_create_profiles.rb:4:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)'

控制器/ application_controller.rb

class Profile < ActiveRecord::Base
belongs_to :user
end

profiles_controller.rb:

class ApplicationController < ActionController::Base

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def after_sign_in_path_for(resource)
    profiles_path(resource)
  end

  def after_sign_up_path_for(resource)
    profiles_path(resource)
  end
end

routes.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
    @profile = Profile.find_by user_id: current_user.id
   @attributes = Profile.attribute_names - %w(id user_id created_at updated_at)
  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 :show, status: :created, location: @profile }
      else
        format.html { render :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 { render :show, status: :ok, location: @profile }
      else
        format.html { render :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, notice: 'Profile was successfully destroyed.' }
      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])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def profile_params
      params.fetch(:profile, {})
    end

参考:stackoverflow.com/questions/21976002/ruby-on-rails-4-devise-and-profile-pages

我搭建了个人资料......任何帮助?我认为它试图创建另一个配置文件表而不是更新... ??

这是我在本地主机上的错误看起来..

click here for the image

0 个答案:

没有答案