我有一个表格来更改密码,提交表格后应该发出一个看跌期权。
<%= form_tag authors_change_password_path, method: :put do %>
<%= fields_for :author do |f| %>
<div class="col-md-6">
<div class="card" style="width: 20rem;">
<div class="card-block">
<div class="card-title">
Change password </div>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
<div class="field">
<%= f.label :current_password %>
<%= f.password_field :current_password, class: 'form-control' %>
</div>
</li>
<li class="list-group-item">
<div class="field">
<%= f.label :new_password %>
<%= f.password_field :new_password, class: 'form-control' %>
</div>
</li>
<li class="list-group-item">
<div class="field">
<%= f.label :new_password_confirmation %>
<%= f.password_field :new_password_confirmation, class: 'form-control' %>
</div>
</li>
<li class="list-group-item">
<%= f.submit "Change password", class: 'btn btn-warning' %></li>
</ul>
</div>
</div
<% end %>
<% end %>
当我点击提交时,我收到以下错误:
Routing Error
No route matches [POST] "/authors/change_password"
我无法弄清楚它为什么发出POST请求,因为我在选项哈希“method :: put”中声明了行
<%= form_tag authors_change_password_path, method: :put do %>
我在这一行尝试了以下变体:
<%= form_tag (authors_change_password_path, method: :put) do %>
<%= form_tag authors_change_password_path, {method: :put} do %>
<%= form_tag (authors_change_password_path, {method: :put}) do %>
<%= form_tag authors_change_password_path, method: "put" do %>
我的路线:
Rails.application.routes.draw do
devise_for :authors
root to: 'blog/posts#index'
namespace :authors do
get '/account' => 'accounts#edit', as: :account
put '/info' => 'accounts#update_info', as: :info
put '/change_password' => 'accounts#change_password', as: :change_password
resources :posts do
put 'publish' => 'posts#publish', on: :member
put 'unpublish' => 'posts#unpublish', on: :member
end
end
scope module: 'blog' do
get 'about' => 'pages#about', as: :about
get 'contact' => 'pages#contact', as: :contact
get 'posts' => 'posts#index', as: :posts
get 'posts/:id' => 'posts#show', as: :post
end
我哪里错了?这很奇怪,因为我遵循教程,我有与教程相同的代码,但我得到错误,教程工作正常。 这是代码。 https://github.com/StephenFiser/FrogBlog/blob/master/config/routes.rb 和视频链接供参考:https://www.youtube.com/watch?v=JP31T66tU7w
编辑:PUT请求只是另一种形式的POST。但实际上它正在进行PUT调用。只需检查生成的HTML表单即可。您将在Rails维护的表单中找到隐藏字段以进行PUT调用。 不要担心你实际上是在幕后进行PUT调用。
如果出现混淆,上面的编辑不是我的作者。
编辑2:表单的html输出
<form action="/authors/change_password" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="put"><input type="hidden" name="authenticity_token" value="pU1oE+BAJjNF+bfSaGqaY27ECYoNfu8K6u71WLUNjV316po8HykOqib0ghhYTd9jD7nV8z+JrMvXO9VSujMXiA==">
<div class="col-md-6">
<div class="card" style="width: 20rem;">
<div class="card-block">
<div class="card-title">
Change password </div>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
<div class="field">
<label for="author_current_password">Current password</label>
<input class="form-control" type="password" name="author[current_password]" id="author_current_password">
</div>
</li>
<li class="list-group-item">
<div class="field">
<label for="author_new_password">New password</label>
<input class="form-control" type="password" name="author[new_password]" id="author_new_password">
</div>
</li>
<li class="list-group-item">
<div class="field">
<label for="author_new_password_confirmation">New password confirmation</label>
<input class="form-control" type="password" name="author[new_password_confirmation]" id="author_new_password_confirmation">
</div>
</li>
<li class="list-group-item">
<input type="submit" name="commit" value="Change password" class="btn btn-warning" data-disable-with="Change password"></li>
</ul>
</div>
</div></form>