2

我正在使用设计,我正在尝试创建一个 CRUD 界面。所以我做到了,但是我似乎无法更新甚至访问表单。

这是我的路线。

devise_for :users, :controllers => { :registrations => "registrations" }
resources :myusers

这里 myuser 控制器编辑、更新

  # POST /myusers
  # POST /myusers.json
  def create
    @myuser = User.new(params[:myuser])

    respond_to do |format|
      if @myuser.save
        format.html { redirect_to @myuser, notice: 'User was successfully created.' }
        format.json { render json: @myuser, status: :created, location: @myuser }
      else
        format.html { render action: "new" }
        format.json { render json: @myuser.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /myusers/1
  # PUT /myusers/1.json
  def update
    @myuser = User.find(params[:id])

    respond_to do |format|
      if @myuser.update_attributes(params[:myuser])
        format.html { redirect_to root_url, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @myuser.errors, status: :unprocessable_entity }
      end
    end
  end

在这里我的编辑表单尝试 1

<%= form_for @myuser, url: myuser_path(@myuser), html: { method: :put} do |f| %>
...
<% end %>

试试2是这个

<%= form_for(@myuser) do |f| %>
...
<% end %>

因为一个似乎确实有效,但它实际上并没有修改数据,它确实显示了它被修改的消息。

更新

这是我的注册控制器:

class RegistrationsController < Devise::RegistrationsController
  def update
    # 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

    @user = User.find(current_user.id)
    if @user.update_attributes(params[:user])
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end
end

我也有注册助手

module RegistrationsHelper
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

这是我的参数

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"Fx7uCaiLyyT3wwnL9GrZ8671oJVeuYxodN+vXqpbqIE=",
 "user"=>{"username"=>"jf_dufour",
 "first"=>"Jean-Sebastien",
 "last"=>"Dufour",
 "phone"=>"...",
 "email"=>"jf_dufour@....ca",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "admin"=>"Technician"},
 "commit"=>"Update User",
 "id"=>"1"}
4

2 回答 2

0

我要在这里刺一下,并说这resources :myusers是导致您的问题的原因。

由于您的奇异模型已命名User,因此您想要 - resources :users

/users/唯一的问题是,如果您愿意,这会将您的路径路由到/myusers/我认为您必须使用单数路由。我不相信这resources允许您指定不同的 url。

于 2013-10-11T15:30:14.340 回答
0

您的创建操作看起来不错,只需确保在表单中包含字段:password:password_confirmation.

您的更新操作应如下所示:

# app/controllers/my_users_controller.rb

def update
  user = User.find(params[:id])

  if params[:myuser][:password].blank?
    params[:myuser].delete(:password)
    params[:myuser].delete(:password_confirmation)
  end

  if user.update_attributes(params[:myuser])
    redirect_to my_users_path, notice: "User has been updated."
  else
    render action: 'edit'
  end
end
于 2013-10-11T15:32:12.930 回答