我有一个带有页面的rails应用程序,供用户编辑自己的个人资料
app/views/manage_users/edit.html.erb
和app/views/manage_users/new.html.erb
包含:
<%= render 'form' %>
app/views/manage_users/_formt.html.erb
包含:
<%= form_for(@user, :as => :user, :url => {:action => @form_action, :id => @user.id}) do |f| %>
当我启动页面http://localhost:3000/manage_users/2/edit
时,它会显示一个编辑用户对象数据的典型表单。如果我删除电子邮件地址http://snag.gy/atnmV.jpg并提交表单,则会收到错误消息,我预计会http://snag.gy/NRfwn.jpg,而网址现在为http://localhost:3000/manage_users/2
:
Started PUT "/manage_users/2" for 127.0.0.1 at 2014-01-25 21:01:45 -0600
Processing by ManageUsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"B44/1b5m8usyAfe0hzLHNyjk/7Fpn5iEu3u6wGJMGL0=", "user"=>{"user_details_attributes"=>{"first_name"=>"Jeff", "last_name"=>"Smith", "id"=>"2"}, "email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "button"=>"", "id"=>"2"}
如果我重新输入电子邮件地址并提交表单,则网址现在指向http://localhost:3000/manage_users?id=2
,我收到错误No route matches [PUT] "/manage_users"
为什么要这样做以及如何解决它。如果我只是去初始页面编辑用户并立即保存(而不是删掉电子邮件),一切正常。
应用程序/控制器/ manage_users_controller.rb
class ManageUsersController < ApplicationController
before_filter :admin_only, :except => [:edit, :update]
# GET /manage_users
# GET /manage_users.json
def index
@users = User.active
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
# GET /manage_users/new
# GET /manage_users/new.json
def new
@user = User.new
@user.build_user_details
@form_action = 'create'
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
# GET /manage_users/1/edit
def edit
@user = User.find(params[:id])
@permissions_disabled = params[:id].to_i == current_user.id.to_i
#p @permissions_disabled
able_to_edit_profile?
session[:return_to] ||= request.referer
@form_action = 'update'
end
# POST /manage_users
# POST /manage_users.json
def create
@user = User.new(params[:user])
#p "in create"
respond_to do |format|
if @user.save
format.html {
flash[:notice] = 'User was successfully created.'
redirect_to(:action => :index)
}
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new", notice: 'Error creating user.' }
format.json { render json: @user, status: :unprocessable_entity }
end
end
end
# PUT /manage_users/1
def update
@user = User.find(params[:id])
able_to_edit_profile?
# required for settings form to submit when password is left blank
if params[:user][:password].blank?
params[:user].delete("password")
params[:user].delete("password_confirmation")
end
respond_to do |format|
if @user.update_attributes(params[:user])
@user.save
# sign the user in with their new password so it doesn't redirect to the login screen
if current_user == @user
sign_in @user, :bypass => true
end
format.html {
p "in success format.html"
flash[:notice] = 'User was successfully updated.'
redirect_to session.delete(:return_to)
}
else
p "in else"
format.html { render action: "edit", notice: 'Error updating user.' }
#format.html {
#flash[:notice] = @user.errors
# redirect_to edit_manage_user_path(@user)
#}
end
end
end
private
# If the user is not an admin and trying to edit someone else's profile, redirect them
def able_to_edit_profile?
if !current_user.try(:admin?) && current_user.id != @user.id
flash[:alert] = "That area is for administrators only."
redirect_to :root
end
end
end
修改
所以改变这个:
format.html { render action: "edit", notice: 'Error updating user.' }
为:
format.html {
flash[:notice] = @user.errors.full_messages.to_sentence
redirect_to edit_manage_user_path(@user)
}
我能够绕过这个问题。我仍然很好奇为什么在更新失败后渲染“编辑”不起作用。
答案 0 :(得分:0)
当您从update方法呈现编辑操作时(在失败路径中),它不会运行edit方法,因此在呈现表单时@form_action为nil。你需要解决这个问题。
我通常不需要设置网址,只有在极少数情况下才能设置网址。您可以将其保留,并让rails处理网址和HTTP方法。