RubyonRails:如何通过用户界面中的按钮将特定/选定的记录从一个表(User)发送到另一个表



我一周前开始使用Ruby。我的项目是制作一个餐厅预订网页。我制作了包含用户名/姓氏/地址/电子邮件/密码/密码确认信息的用户表,我还制作了另一个名为"朋友"的表,其中包含姓名/姓氏/住址/电子邮件。

当按下所有现有用户表中用户行的按钮(添加)时,当前用户可以将朋友添加到朋友表中。因此,通过点击当前用户选择的特定用户信息按钮,特定用户信息(姓名/姓氏/地址/电子邮件)将复制到当前用户朋友表中。

我使用的数据库是Sqlite。

这是users_controller:

class UsersController < ApplicationController
  before_filter :save_login_state, :only => [:new, :create]
  def index
    @user = User.all
  end
  def new
      #Signup Form
      @user = User.new     
  end
  def show
    redirect_to(:controller => 'sessions', :action => 'login')
    flash[:notice] = "Successful!"
    flash[:color]= "valid"
  end
  def edit
    @user = User.find(params[:id])
  end
  def update
    @user = User.find(params[:id])
    if @user.update(user_params)
        redirect_to @user
    else
        render 'edit'
    end
  end
   def create
        @user = User.new(user_params)
        if @user.save
            redirect_to(:action => 'login')
        else
            flash[:notice] = "Form is invalid"
            flash[:color]= "invalid"
            render "new"
        end 
    end
  private
  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation, :ime, :prezime, :adresa)
  end
end

用户index.html.erb:

<table class="table table-striped sortable">
  <thead>
  <tr class="tr1">
    <th class="th2">E-mail</th>
    <th class="th2">Ime</th>
    <th class="th2">Prezime</th>
    <th class="th2">Adresa</th>
  </tr>
  </thead>
  <tbody>
  <% @user.each do |users| %>
    <tr class="tr1">
      <td><%= users.email %></td>
      <td><%= users.ime %></td>
      <td><%= users.prezime %></td>
      <td><%= users.adresa %></td>
      <td><%= link_to 'Edit', edit_user_path(users)%></td>
    </tr>
  <% end %>
  </tbody>
</table>

用户new.html.erb:

<% @page_title = "UserAuth | Signup" %>
<div class="Sign_Form">
  <h1>Registracija</h1>
  <%= form_for(@user) do |f| %>
    <table class="table4">
    <tr><th class="th1"> Email: </th><th><%= f.text_field :email%></th></tr>
    <tr><th class="th1"> Password: </th><th><%= f.password_field :password%></th></tr>
    <tr><th class="th1"> Repeat password: </th><th><%= f.password_field :password_confirmation%></th></tr>
    <tr><th class="th1"> Ime: </th><th><%= f.text_field :ime%></th></tr>
    <tr><th class="th1"> Prezime: </th><th><%= f.text_field :prezime%></th></tr>
    <tr><th class="th1"> Adresa: </th><th><%= f.text_field :adresa%></th></tr>
    </table>
    <div align="left"><%= f.submit :"Sign Up" %></div>
  <% end %>
  <% if @user.errors.any? %>
    <ul class="Signup_Errors">
    <% for message_error in @user.errors.full_messages %>
      <li>* <%= message_error %></li>
    <% end %>
    </ul>
  <% end %>
</div>

user.rb:

class User < ActiveRecord::Base
  attr_accessor :password
  before_save :encrypt_password
  after_save :clear_password
  EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/
  validates :ime, :presence => true
  validates :prezime, :presence => true
  validates :adresa, :presence => true
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :password, :presence => true, length: { minimum: 6 }, :confirmation => true
  #Only on Create so other actions like update password attribute can be nil
  #attr_accessible :username, :email, :password, :password_confirmation
  def self.authenticate(email="", login_password="")
    if  EMAIL_REGEX.match(email)    
      user = User.find_by_email(email)
    end
    if user && user.match_password(login_password)
      return user
    else
      return false
    end
  end   
  def match_password(login_password="")
    encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
  end
  def encrypt_password
    unless password.blank?
      self.salt = BCrypt::Engine.generate_salt
      self.encrypted_password = BCrypt::Engine.hash_secret(password, salt)
    end
  end
  def clear_password
    self.password = nil
  end
end

create_users:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
        t.string :email
        t.string :encrypted_password 
        t.string :salt
        t.string :ime
        t.string :prezime
        t.string :adresa
        t.timestamps
      t.timestamps null: false
    end
  end
end

Friendships需要一个控制器,例如FriendshipsController。您的Add按钮应指向FriendshipsController中的一个操作,该操作将复制您提供的参数。

  • 在你看来,你应该有这样的东西:

    <%= button_to "Add friend", friendships_path(:name => user.name, :email => user.email ...) %>

  • 友情控制器:

    def create # handle params here (you can get user's data from params[:name], params[:email] and such # e.g. @friendship = Friendship.create(:name => params[:name], ...) end

也可以考虑这篇文章http://railscasts.com/episodes/163-self-referential-association解释Rails中的自引用关联。

相关内容

最新更新