简体   繁体   中英

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. 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.

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

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

<% @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

You will need a controller for Friendships eg FriendshipsController . Your Add button should point to an action in FriendshipsController which will copy the parameters you provide.

  • In your view you should have something like this:

    <%= 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

Also consider this article http://railscasts.com/episodes/163-self-referential-association explaining self referential association in Rails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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