简体   繁体   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

I started off with Ruby one week ago. 我一周前开始使用Ruby。 My project is to make a Restaurant reservation webpage. 我的项目是制作一个餐厅预订网页。 I made the User table that contains Users name/last name/address/email/password/password confirmation info, I also made another table called Friends that contains name/last name/address/email. 我创建了包含用户名/姓氏/地址/电子邮件/密码/密码确认信息的用户表,还创建了另一个名为“朋友”的表,其中包含姓名/姓氏/地址/电子邮件。

The current user is able to add a friend to the Friend table when pressing a Button (Add) by the users row in the table of all existing Users. 通过按所有现有“用户”表中的“用户”行中的按钮(添加),当前用户可以将朋友添加到“朋友”表中。 So, by clicking the button by the specific user info that the current user selected, the specific user info (name/last name/address/email) will be copied to the current user friend table. 因此,通过单击当前用户选择的特定用户信息的按钮,特定用户信息(名称/姓氏/地址/电子邮件)将被复制到当前用户好友表中。

The database I am using is Sqlite. 我正在使用的数据库是Sqlite。

Here is the users_controller: 这是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

User index.html.erb: 用户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>

User new.html.erb: 用户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: 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: 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

You will need a controller for Friendships eg FriendshipsController . 您将需要一个用于Friendships的控制器,例如FriendshipsController Your Add button should point to an action in FriendshipsController which will copy the parameters you provide. 您的“ Add按钮应指向FriendshipsController中的一个动作,该动作将复制您提供的参数。

  • In your view you should have something like this: 在您看来,您应该具有以下内容:

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

  • FriendshipsController: 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

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

暂无
暂无

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

相关问题 Rails 4-如果在另一个表中不是特定记录,如何从一个表中选择数据? - Rails 4 - How to select data from a table if in another table is not a specific record? 如何从控制器Ruby On Rails中传递表中的用户ID - How to pass user id in table from controller Ruby On Rails 如何将ID从一张桌子存储到另一张桌子上的红宝石在Rails 2上 - How to store the id from the one table to another in ruby on rails 2 如何用Ruby on Rails控制器中另一条记录的数据替换一条记录中的数据? - How to replace data in one record with data from another record in a controller in Ruby on Rails? 如何从一个用户向另一个用户发送Word文件 - How to Send a word file from one user to another Ruby on Rails:如何从登录弹出窗口和用户给定的凭证将AJAX请求发送到Rails控制器 - Ruby on rails: How to send an AJAX request to rails controller from login popup along with user given credentials 如何阻止一个特定用户在Ruby on Rails中删除其帐户? - How to stop one specific user to delete his account in Ruby on Rails? 如何将Ruby on Rails Heroku应用程序的相关用户信息(不是全部)从一个生产数据库复制到另一个数据库 - How do I copy relevant user information (not all of it) from one production database to another for a Ruby on Rails Heroku application rails将记录从一个表插入到具有匹配属性的另一个表 - rails insert record from one table to another table with matched attributes 我如何在另一个表红宝石上自我引用用户 - How can i self reference user in another table ruby on rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM