一周前我开始使用Ruby。我的项目是制作餐厅预订网页。我制作了包含用户名/姓氏/地址/电子邮件/密码/密码确认信息的用户表,我还制作了另一个名为Friends的表,其中包含姓名/姓氏/地址/电子邮件。
当按下所有现有用户的表格中的用户行按钮(添加)时,当前用户可以将朋友添加到朋友表。因此,通过按当前用户选择的特定用户信息单击按钮,特定用户信息(姓名/姓氏/地址/电子邮件)将被复制到当前用户朋友表。
我使用的数据库是Sqlite。
这是users_controller:
<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>
用户index.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>
用户new.html.erb:
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
user.rb:
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
create_users:
UIScrollView
答案 0 :(得分:0)
您需要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
# e.g. @friendship = Friendship.create(:name => params[:name], ...)
end
另请考虑本文http://railscasts.com/episodes/163-self-referential-association解释Rails中的自引用关联。