路由错误:没有路由匹配{:controller =>" ...",:action =>" ..."}

时间:2014-11-14 08:14:21

标签: ruby-on-rails ruby

我尝试使用一些代码进行简单搜索,但我无法使用此错误运行代码:

Routing Error
No route matches {:controller=>"welcome", :action=>"test"}
Try running rake routes for more information on available routes.

这是我的控制器

  def search
    @users = User.search(params[:search])
    if @users
      redirect_to welcome_test_path(@users)
    else
      render root
    end
  end

  def test
    @user = User.find(params[:id])
    logger.debug "user: #{@user}"
  end

HTML:

  <%= form_tag :controller => 'welcome', :action => 'search', :method => 'post' do %>
  <%= text_field_tag :search, params[:search], :id => 'search_field' %>                     
  <%= submit_tag "Search", :name => nil %>
  <% end %>

我的路线

  get "uploads/new"

  get "micropost/new"

  get "user/new"
  get "user/saved"

  get "post/new"
  get "post/show"

  get "welcome/index"
  get "welcome/sucess"
  get "welcome/edit"
  get "welcome/test"
  root :to => "welcome#index"

  resources :users
  resources :relations,  only: [:create, :destroy]
  resources :microposts
  resources :uploads


  get '/users/:id', :to => 'welcome#sucess', :as => "user"  
  match '/relations', to: 'relation#create', via: 'post'
  match '/relations/:id', to: 'relation#destroy', via: 'delete'


  # Search
  match '/welcome/search', to: 'welcome#search', via: 'post'
  get '/welcome/test/:id', :to => 'welcome#test', :as => 'welcome_test'

  #Edit  
  match '/welcome/edit', to: 'welcome#edit', via: 'post'

  #Login & Logout
  match '/login', to: 'welcome#create', via: 'post'
  match '/logout' => 'welcome#destroy', as: :logout

  #Create
  match '/create', to: 'micropost#create', via: 'post'
  match '/signup', to: 'user#signup', via: 'post'

enter image description here

如果我删除&#34;:as =&gt; &#39; welcome_test&#34;在我的搜索路线中,我的应用程序将运行,但我无法获得用户ID,因此我必须将其放入我的路线或尝试其他方式来获取用户ID。在这里,如果我删除:as => 'welcome_test

,这是我的日志文件
Started POST "/welcome/search?method=post" for 127.0.0.1 at 2014-11-14 15:02:21 +0700
Processing by WelcomeController#search as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ih08q0JhsrhjEm8G/3AEqnxZZZbw8VvM+VWGP1lUl/8=", "search"=>"sa1234", "method"=>"post"}
  [1m[36mUser Load (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE (username LIKE '%sa1234%' OR age LIKE '%sa1234%')[0m
user: [#<User id: 10, username: "sa1234", pass: "1234", created_at: "2014-10-14 09:12:44", updated_at: "2014-11-13 10:25:26", age: 18, image: "Hydrangeas.jpg">]
Redirected to http://localhost:3000/welcome/test.10
Completed 302 Found in 3.0ms (ActiveRecord: 0.0ms)


Started GET "/welcome/test.10" for 127.0.0.1 at 2014-11-14 15:02:22 +0700
Processing by WelcomeController#test as 
Completed 404 Not Found in 1.0ms

ActiveRecord::RecordNotFound (Couldn't find User without an ID):
  app/controllers/welcome_controller.rb:43:in `test'

所以,请帮我解决这个问题! 我应该删除:as => 'welcome_test并尝试使用另一种方式来访问ger user_id,或者修复我的路由?

0 个答案:

没有答案