0

我已经按照 Hartl 的 Rails 教程学习了第 9 章,现在我正在构建自己的想法,以便深入了解 Rails。

当前问题 - 我在编辑中向用户个人资料添加了一个简历,并且文本框出现在用户个人资料上,但我不知道如何保存添加到个人简历文本框中的文本。我已经生成了一个名为“add_bio_to_user_profile”的迁移(还没有耙过),但我正在努力弄清楚要添加到 User.rb 模型中的内容。控制器进入这个?

迁移“add_bio_to_user_profile”

   class AddBioToUserProfile < ActiveRecord::Migration
  def change
    add_column :user_profiles, :, :string
  end
end

模型/用户.rb

class User < ActiveRecord::Base

  before_save { self.email = email.downcase }
  before_create :create_remember_token

  validates :name, presence: true, length: { maximum: 50 }
  #VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    #format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private

    def create_remember_token
      self.remember_token = User.encrypt(User.new_remember_token)
    end
end

控制器/user_controller.rb

class UsersController < ApplicationController
  before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

def index
    @users = User.paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User destroyed."
    redirect_to users_url
  end


  def create
    @user = User.new(user_params)
      if @user.save
        sign_in @user
        flash[:success] = "Welcome to the Sample App!"
        redirect_to @user
      else
        render 'new'
      end
  end

  def edit
  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

    # Before filters

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end
end

显示.html.erb

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="span4">
    <section>
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>

        <div>
            <textarea rows="3"></textarea>
            </div>

  </aside>
</div>

编辑.html.erb

<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :bio %>
      <textarea rows="3"></textarea>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirm Password" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
    <% end %>

    <%= gravatar_for @user %>
    <a href="http://gravatar.com/emails">change</a>
  </div>
</div>
4

2 回答 2

0

我猜您想将 bio 字段添加到 users 表中。这里是迁移。

class AddBioToUserProfile < ActiveRecord::Migration
  def change
    add_column :users, :bio, :string
  end
end

当您进行迁移时,只有模型才能发挥作用。其中, users是您的表名,bio是新列,string是数据类型。

于 2013-09-13T10:56:33.337 回答
0

形式上,你不必做任何事情。Rails 魔法从这里开始。您运行迁移,如果您正在运行生产环境,则重新启动服务器,在编辑 html.erb 中将文本框的名称更改为 :bio

<%=f.text_field :bio%>

提交此表单后,它会自动保存在表格中。此外,您可以在模型中的此字段上添加验证。

在 edit.html.erb 中,进行此更改

  <%= f.label :bio %>
  <%=f.text_field :bio%>

在 show.html.erb 中

    <%= gravatar_for @user %>
    <%= @user.name %>
  </h1>
Bio: <%= @user.bio %>

controller.rb 中的一项更改

def user_params
params.require(:user).permit(:name, :email, :bio, :password,
                                       :password_confirmation)
end
于 2013-09-13T11:00:38.463 回答