Ruby on Rails POST请求失败

时间:2017-04-06 10:16:24

标签: ruby-on-rails forms ruby-on-rails-4 post

事情就是这样,我想在Ruby on Rails的页面中写一个登录页面,当网址是localhost:3000/admin时,我会得到需要填写的页面。表单填写完成后在,我提交了按钮,表单应该通过create方法接受post操作,但它仍适用于get方法和new操作。

它会发生什么?

以下是routes.rb文件:

     Prefix Verb          URI Pattern                Controller#Action
new_admin_users GET      /admin/users/new(.:format)  admin/users#new
   admin_users  POST     /admin/users(.:format)      admin/users#create
         admin  GET      /admin(.:format)            admin/users#new
                POST     /admin(.:format)            admin/users#create
    posts_home  GET      /posts/home(.:format)       redirect(301, /posts)
   posts_about  GET      /posts/about(.:format)      redirect(301, /about)

然后这是新动作

  <form class="form-signin">
    <h2 class="form-signin-heading">Please sign in</h2>
    <%= form_for :user,url:admin_users_path do |f| %>
      <p>
        <%= f.label :name%><br>
        <%= f.text_field :name ,class:"form-control" %>
      </p>
      <p>
        <%= f.label :pass%><br>
        <%= f.text_field :pass,class:"form-control" %>
      </p>
      <div class="checkbox">
        <label>
          <input type="checkbox" value="remember-me"> Remember me
        </label>
      </div>
      <p>
        <%= f.submit'Sign in', :class=>"btn btn-lg btn-primary btn-block" %>
      </p>
      <%= link_to 'Back', posts_path %>
    <%end%>
  </form>

以下是routes.rb

   Rails.application.routes.draw do
     namespace :admin do
       resources :users
       get "",  to:"users#new"
       post "", to:"users#create"
     end
     get "/posts/home",  to: redirect("/posts")
     get "/posts/about", to: redirect("/about")
     resources :posts do
       resources :comments
     end
     root "posts#index"
     get "/home",to: "posts#index"
     get "/about",to: "posts#about"
   end

如果你有一些经历,请提前告诉我,谢谢!

2 个答案:

答案 0 :(得分:0)

您必须在控制器内部构建实际的User对象。尝试在表单中使用它而不是符号:user

<%= form_for @user, url: admin_users_path do |f| %>
...

答案 1 :(得分:-1)

您可以尝试明确指定方法帖子 而不是

RelayCommand

使用此:

<%= form_for :user,url:admin_users_path do |f| %>

同时检查以这种方式创建的表单中创建的操作

希望这有助于