![](/img/trans.png)
[英]Custom user page for rails using devise - Using Rails 4 and Ruby - 2.2
[英]Getting my Ruby on Rails page to save the user's email without using devise
我正在建立一個一頁的網站,訪客可以在其中簡單地提交其電子郵件地址。 數據庫中的唯一目標是獲取電子郵件(無姓名等)。 最初只有一個頁面,即主頁。 如果用戶提交已在使用中的電子郵件,則會將用戶發送到錯誤頁面。 如果未使用該電子郵件,則會將用戶發送到成功頁面。
之前,我曾問過一個問題,經過大量評論和反復試驗后,看來它起作用了,然后又停止了工作。 當我使用Rails C時,系統中只有一個用戶,並且該用戶沒有電子郵件...
這是我的用戶遷移的樣子:
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :email
t.timestamps
end
add_index :users, :email, unique: true
end
end
我的用戶模型如下所示:
class User < ApplicationRecord
end
這是users / new.html.erb的樣子:
<%= form_for @user, as: :post, url: users_path do |f| %>
<div class="wrapper">
<%= f.email_field :email , id: "search", class:"search input" %> <br />
<%= f.submit "yep", class: "submit input" %>
</div>
<% end %>
這是我的用戶控制器:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:email])
if @user.save
redirect_to '/users/success'
else
redirect_to '/users/error'
end
end
def show
end
end
這是我的路線:
Rails.application.routes.draw do
root "users#new"
resources :users
end
當我運行代碼時,它將呈現主頁,但是當我單擊“提交”時,它會將我發送到一個名為show.html.erb的頁面,其中我的Brownser上帶有http:// localhost:3000 / users / error 。 沒有用戶被保存在控制台中。
編輯:我的模型是
class User < ApplicationRecord
validates_uniqueness_of :email
end
它仍然無法正常工作。
將new.html.erb更改為
<%= form_with(model: @user, local: true) do |f| %>
<div class="wrapper">
<%= f.email_field :email , id: "search", class:"search input" %> <br />
<%= f.submit "yep", class: "submit input" %>
</div>
您的控制器將
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to user_path(@user), notice: "yeh!!!!"
else
redirect_to new_user_path, notice: "email already registered"
end
end
def show
@user = User.find(params[:id])
end
private
def user_params
params.require(:user).permit(:email)
end
end
加
在布局中<p id="notice"><%= notice %></p>
到您的application.html.erb
休息作為您的問題
這里有幾處錯誤。
您是如此親密,但是您濫用了form_for
的as:
屬性。 也許您認為它將作為 POST
請求發送,但實際上是將表單參數包裝在稱為“ post”的對象中。 我在另一個線程的注釋中看到了這一點。
刪除as:
屬性,幫助程序將再次將參數包裝在user
對象中。 在使用它的同時,您還應該能夠刪除url:
屬性,因為Rails表單幫助程序足夠聰明,可以推斷出這是一個新的資源豐富的記錄,並相應地輸出create
URL和POST
操作。
您需要控制器期望整個“用戶”對象,而不只是檢查email
參數。 另外,假設您使用的是Rails 4或更高版本,則需要允許在您的User
對象上批量分配email
屬性。 參見代碼。
def create
@user = User.new(params.require(:user).permit(:email)) # Not params[:email]
if @user.save
redirect_to '/users/success'
else
redirect_to '/users/error'
end
end
class User < ApplicationRecord
validates :email, uniqueness: { case_sensitive: false }
end
獎金!
如今,最好移到form_with
(而不是form_for
)。 它正在成為新的Rails標准的途徑,並且使其中的一些事情變得更加容易。 您要記住的一點是,在form_with
(以及一般的Rails假設)下, 默認情況下 ,窗體是遠程的 。 因此,如果要觸發整頁提交/刷新,請在您的form_with
幫助器中添加local: true
。
<%= form_with model: @user, local: true do |f| %>
<div class="wrapper">
<%= f.email_field :email , id: "search", class:"search input" %> <br />
<%= f.submit "yep", class: "submit input" %>
</div>
<% end %>
嘗試在模型中添加validates_uniqueness_of
class User < ApplicationRecord::Base
attr_accessor :email
validates_uniqueness_of :email
end
和
def show
@user = User.find(email: params[:email])
end
如果你想檢查所有
def show
@user = User.all
end
請嘗試這個。
希望對您有所幫助
當您在路由中使用資源時,當route為/users/:id
時將調用def show
。 這就是為什么它調用show.html.erb文件的原因。
當您嘗試驗證電子郵件時,請在模型中為其編寫驗證
class User < ApplicationRecord
validates_uniqueness_of :email
end
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.