我有一个控制器“find_numbers”,我用它来向 Twilio API 提交表单。不过,在提交之前,我想针对两个表单字段进行验证,这两个表单字段不在此控制器的数据模型中。字段是 :name 和 :original_number
因此,在我的 find_numbers 模型中,我添加了 attr_accessor :name, attr_accessor :originial number 以在其下运行 validates 命令。
完成此操作并将表单提交为无效后,我收到错误消息:
Routing Error
No route matches {:controller=>"phone", :action=>"new"}
Try running rake routes for more information on available routes.
我不确定为什么它说没有根,但我不确定它为什么要访问它。我希望它发布到 find_numbers
find_numbers/新模板
<%= form_tag("/find_numbers", :method => "post", :id => "new_user" ) do %>
<%= render 'shared/error_messages' %>
<%= label_tag(:name, "What Are You Tracking?") %>
<%= text_field_tag(:name) %>
<%= label_tag(:original_number, "Your Orginal Number") %>
<%= text_field_tag(:original_number) %>
<%= label_tag(:in_postal_code, "Near US postal code (e.g. 94117):") %>
<%= text_field_tag(:in_postal_code) %>
<%= label_tag(:near_number, "Near this other number (e.g. +4156562345)") %>
<%= text_field_tag(:near_number) %>
<%= label_tag(:contains, "Matching this pattern (e.g. 415***EPIC):") %>
<%= text_field_tag(:contains) %>
<%= submit_tag("Search", :class => "btn btn-large btn-primary") %>
<% end %>
这是我的 find_number 模型
class FindNumber < ActiveRecord::Base
attr_accessor :name
attr_accessor :original_number
validates :name, presence: true
validates :original_number, presence: true
end
这是我的 Find_number 控制器
class FindNumbersController < ApplicationController
def new
@user = current_user
end
def create
@user = current_user
client = Twilio::REST::Client.new(@user.twilio_account_sid, @user.twilio_auth_token)
search_params = {}
%w[in_postal_code near_number contains].each do |p|
search_params[p] = params[p] unless params[p].nil? || params[p].empty?
end
local_numbers = client.account.available_phone_numbers.get('US').local
@numbers = local_numbers.list(search_params)
unless @numbers.empty?
render 'find_numbers/show'
else
flash.now[:error] = "Sorry, We Couldn't Find Any Numbers That Matched Your Search! Maybe Something Simpler?"
render 'find_numbers/new'
end
end
def show
end
end
任何关于实现这一点的想法将不胜感激!
编辑
Routes.rb 文件
Dct::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :phones, only: [:new, :create, :destroy]
resources :find_numbers, only: [:new, :create, :destroy]
match '/find_numbers', to: 'find_numbers#new'
match '/signup', to: 'users#new'
match '/login', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
match '/product_demo', to: 'static_pages#product_demo'
match '/pricing', to: 'plans#index'
match '/contact', to: 'static_pages#contact'
编辑
这是服务器日志,当我点击提交时发生了什么
http://stepanp.com/railserror.jpg
另外,这里是 find_numbers/show 视图