[英]Ruby on Rails POST request fails
Here the thing goes, I wanna write a login in a page in Ruby on Rails, when the url is localhost:3000/admin
, I'll get the page which need to fill in. After the form has filled in, I submit the button and the form should be admitted to the create
action with post
method, however it still works with get
method and new
action. 事情就这样了,我想在Ruby on Rails的页面中写一个登录名,当url为localhost:3000/admin
,我将得到需要填写的页面。填写完表单后,我提交按钮和表单应允许使用post
方法进行create
操作,但是仍可与get
方法和new
操作一起使用。
What happens to it? 发生什么事了?
Here is the routes.rb
file: 这是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)
Then here is the new action 然后这是新动作
<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>
Here is the routes.rb
: 这是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
If you have some throught, please tell me, thanks in advance! 如果您有意见,请告诉我,谢谢。
You must have the actual User
object built inside your controller. 您必须在控制器内部构建实际的User
对象。 Try using that inside your form instead of symbol :user
. 尝试在表单中使用它,而不是symbol :user
。
<%= form_for @user, url: admin_users_path do |f| %>
...
You can try explicitly specifying the method post Instead of 您可以尝试显式指定方法post而不是
<%= form_for :user,url:admin_users_path do |f| %>
Use this: 用这个:
<%= form_for :user,url:admin_users_path, method: :post do |f| %>
Also check the created action in form created such way 还要以这种方式创建的表单检查创建的动作
Hope this Helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.