虚拟属性:没有路由匹配{:controller =>" phone",:action =>" new"}

时间:2012-10-03 17:04:56

标签: ruby-on-rails ruby

我有一个控制器“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 / new template

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

1 个答案:

答案 0 :(得分:1)

从你发布的内容来看,唯一看起来可疑的东西是你可能有一个PhonesController(复数),因为你已经声明了资源:手机,但路由错误似乎是因为它正在寻找一个PhoneController(单数)。