当我尝试提交表单以编辑用户详细信息时出现此错误。我正在使用Rails 3.2.7。这是我的控制器
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end
def new
@user = User.new
end
def edit
@user = User.find(params[:id])
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the app!"
redirect_to welcome_path
else
render 'new'
end
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
sign_in @user
flash[:success] = "Profile updated"
redirect_to welcome_path
else
render 'edit'
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
end
形式:
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<p><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</p>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="box">
<%= f.label :email %>
<%= f.text_field :email, class:"input_text" %>
<br>
<%= f.label :"edit password" %>
<%= f.password_field :password, class:"input_text" %>
<br>
<label>
<br>
<%= f.submit "Save", class: "button", name: "button" %>
</label>
</div>
<% end %>
Rake路线输出:
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
请告诉我是否还有其他内容我应该发帖...提前感谢
答案 0 :(得分:1)
编辑必须是GET,除了CREATE(POST),UPDATE(PUT)和DESTROY(DELETE)之外,所有方法都是GET。您可以复制应用生成的表单的HTML代码吗?
答案 1 :(得分:0)
您的edit_user路由不应该是POST路由吗?我在这里看到/ users / 1 / edit是一个GET路由。 也许你在config / routes.rb中犯了错误?
你能发布吗?
答案 2 :(得分:0)
通过删除:
来解决此问题gem 'rails-api'
答案 3 :(得分:0)
我发现了同样的错误,我已经解决了这个问题:get和patch的显式格式:
获取'编辑',id:@user
和
patch'update',id:@ user,user:{name:name, 电邮:电邮, 密码:“”, password_confirmation:“”}
使用宝石:
捆绑更新不会在3.4.0到4.1
中更新rspec gems