【问题标题】:Devise rails can't update with CRUD设计 rails 不能用 CRUD 更新
【发布时间】:2013-10-11 15:20:31
【问题描述】:

我正在使用设计,我正在尝试创建一个 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"}

【问题讨论】:

  • 您是否遵循此处的流程:github.com/plataformatec/devise/wiki/… ?如果您没有正确管理密码和密码确认参数,则该过程将失败
  • 如何生成用户控制器?因为那将是我唯一没有做的。我确实有如下编辑注册
  • 在你的情况下,你可以把它放在 MyUsersController 中(你已经发布了你的创建和更新操作)

标签: ruby-on-rails ruby-on-rails-3 devise


【解决方案1】:

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

由于您的奇异模型名为 User,因此您需要 - resources :users

唯一的问题是,如果你想要/myusers/,这会将你的路径路由到/users/ .

【讨论】:

    【解决方案2】:

    您的创建操作看起来不错,只需确保在表单中包含字段: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
    

    【讨论】:

    • 我只是尝试但是我现在收到此错误:未定义方法 `[]' for nil:NilClass
    • 我的参数是 :user 而不是 :myuser... 查看最新更新,有帮助吗?
    • 不,因为我让他们检查为我的用户,设备有时会很痛苦
    • 您使用的是什么版本的 Rails?你在使用strong_parameters吗?我以这种方式使用 Devise,它在 Rails 3 中运行良好。
    • 版本是最新的昨天刚安装的,所以是3.2.13
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    相关资源
    最近更新 更多