繁体   English   中英

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

[英]Ruby on Rails: How to send a specific/chosen record from one table (User) to another with a button from user interface

我一周前开始使用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-z\d\-.]+\.[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 ...) %>

  • FriendshipsController:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM