Rails Model/Database 中搜索的路由和操作逻辑
Rails Routing and Action Logic for Searching in a Model/Database
我正在构建一个根植于索引页面的应用程序,其中有一个按 phone 号码进行客户搜索的表单。我无法确定搜索客户的正确方法以及如何以 RESTFUL 方式路由他们。
我需要通过phone号码查询客户。如果在客户模型(数据库)中找到 phone 号码,它应该 return 客户显示页面。如果找不到客户,即客户不在数据库中,我需要重定向到 new_customer_path,其中有新客户的完整注册表单。
我当前使用的逻辑是为索引搜索表单执行 'post' #create 方法,以通过 phone 号码在数据库中搜索客户。如果找不到客户,那么我的路线会转到 new_customer_path 以创建新客户(我还需要传递他们输入的 phone 号码,以便它自动将其分配给新客户,他们应该'必须在搜索后重新输入他们的 phone 号码)。这里是我再次使用 'post' #create 方法来保存新客户的地方。所以我不确定这是否是正确的方法。
问题 #1:在索引页面上,我的搜索表单是否应该使用 'get' 路径在客户模型中搜索客户?如果是这样,我的表格会是什么样子,那会是什么动作?我尝试通过使用 form_tag 来使用 #show,但您需要有一个客户 ID(我已经在通过 phone 号码开始搜索)。想知道这是否是正确的逻辑,或者是否有更清晰、更简单的逻辑?
索引页:
<%= form_for(Customer.new) do |f| %>
<%= f.label :phone, "Your Phone Number:" %><br>
<%= f.text_field :phone %>
<%= f.submit "Search" %>
<% end %>
控制器:
def new
@customer = Customer.new
end
def create
if @customer = Customer.find_by(phone: customer_params[:phone])
redirect_to @customer
else
@customer = Customer.new(customer_params)
if @customer.save
redirect_to @customer, notice: "Customer was successfully saved"
else
render 'new', notice: "Customer was unsuccessfully saved"
end
end
end
def show
@customer = Customer.find(params[:id])
end
客户模型:
create_table :customers do |t|
t.string :first_name
t.string :last_name
t.string :phone
t.string :email
t.string :zip_code
t.string :birthday
t.timestamps
end
end
假设某人进入索引页面,看到此表格,输入他的号码并继续。他会立即创建新帐户,但他没有提及。
稍后,您肯定会添加对姓名、电子邮件、邮政编码等的验证,至少 :presence=>true
然后,按照这样的逻辑,此人输入他的 phone 号码(如果他不在数据库中),突然出现错误,他的姓名、电子邮件和其他内容不应为空。
这只是几种可能的意外行为方式。
那不是一条路要走。
您应该向控制器添加一些操作,例如:
def find_or_redirect
@customer = Customer.find_by(phone: params[:phone])
if @customer
redirect_to @customer
else
render :new
end
end
使用表单传递参数
<%= form_tag("/find_or_redirect", method: "get") do %>
<%= text_field_tag "phone" %>
<% end %>
你应该在 routes.rb
中提供路线
get '/find_or_redirect' => "customers#find_or_redirect"
这是大概的代码。
我正在构建一个根植于索引页面的应用程序,其中有一个按 phone 号码进行客户搜索的表单。我无法确定搜索客户的正确方法以及如何以 RESTFUL 方式路由他们。
我需要通过phone号码查询客户。如果在客户模型(数据库)中找到 phone 号码,它应该 return 客户显示页面。如果找不到客户,即客户不在数据库中,我需要重定向到 new_customer_path,其中有新客户的完整注册表单。
我当前使用的逻辑是为索引搜索表单执行 'post' #create 方法,以通过 phone 号码在数据库中搜索客户。如果找不到客户,那么我的路线会转到 new_customer_path 以创建新客户(我还需要传递他们输入的 phone 号码,以便它自动将其分配给新客户,他们应该'必须在搜索后重新输入他们的 phone 号码)。这里是我再次使用 'post' #create 方法来保存新客户的地方。所以我不确定这是否是正确的方法。
问题 #1:在索引页面上,我的搜索表单是否应该使用 'get' 路径在客户模型中搜索客户?如果是这样,我的表格会是什么样子,那会是什么动作?我尝试通过使用 form_tag 来使用 #show,但您需要有一个客户 ID(我已经在通过 phone 号码开始搜索)。想知道这是否是正确的逻辑,或者是否有更清晰、更简单的逻辑?
索引页:
<%= form_for(Customer.new) do |f| %>
<%= f.label :phone, "Your Phone Number:" %><br>
<%= f.text_field :phone %>
<%= f.submit "Search" %>
<% end %>
控制器:
def new
@customer = Customer.new
end
def create
if @customer = Customer.find_by(phone: customer_params[:phone])
redirect_to @customer
else
@customer = Customer.new(customer_params)
if @customer.save
redirect_to @customer, notice: "Customer was successfully saved"
else
render 'new', notice: "Customer was unsuccessfully saved"
end
end
end
def show
@customer = Customer.find(params[:id])
end
客户模型:
create_table :customers do |t|
t.string :first_name
t.string :last_name
t.string :phone
t.string :email
t.string :zip_code
t.string :birthday
t.timestamps
end
end
假设某人进入索引页面,看到此表格,输入他的号码并继续。他会立即创建新帐户,但他没有提及。
稍后,您肯定会添加对姓名、电子邮件、邮政编码等的验证,至少 :presence=>true
然后,按照这样的逻辑,此人输入他的 phone 号码(如果他不在数据库中),突然出现错误,他的姓名、电子邮件和其他内容不应为空。
这只是几种可能的意外行为方式。
那不是一条路要走。
您应该向控制器添加一些操作,例如:
def find_or_redirect
@customer = Customer.find_by(phone: params[:phone])
if @customer
redirect_to @customer
else
render :new
end
end
使用表单传递参数
<%= form_tag("/find_or_redirect", method: "get") do %>
<%= text_field_tag "phone" %>
<% end %>
你应该在 routes.rb
中提供路线get '/find_or_redirect' => "customers#find_or_redirect"
这是大概的代码。