尝试更新设备中的用户,在注册/编辑之外呈现表单时遇到问题

时间:2014-10-03 23:12:10

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

我正在尝试从默认注册/编辑视图以外的其他视图更新我的用户。我还没有完整地设置表单,但我开始时遇到路由问题。

这是我的表格:

%section#profile-editor
    .row
      .small-12.columns
        %h2 Edit Profile
    .row
      .small-12.columns
        .well
          =form_tag(edit_thing_registration_path,method: 'PUT', id: "my-awesome-dropzone.dropzone") do 
            .row
              .small-4.columns
                %input#photo-dropzone{:name => "file", :type => "file"}/
              .small-8.columns
                .row
                  .small-12.columns
                    %label{:for => "Name"} Name
                    %input{:name => "Name", :type => "text"}/
                  .small-6.columns
                    %label{:for => "Website"} Website
                    %input{:name => "Website", :type => "text"}/
                  .small-6.columns
                    %label{:for => "Itunes"} Itunes URL
                    %input{:name => "Itunes", :type => "text"}/
                  .small-6.columns
                    %label{:for => "Video"} Youtube Video URL (For Profile)
                    %input{:name => "Video", :type => "text"}/
                  .small-6.columns
                    %label{:for => "Email"} Admin Email
                    %input{:name => "Email", :type => "text"}/
            .row
              .small-12.columns.pad-top
                %label{:for => "RTE"} Content
                %input.jqte{:name => "input", :type => "text", :value => ""}/
            = submit_tag "Submit", class: "button button-green"

这是我的路线:

devise_for :things, controllers: { registrations: "things/devise/registrations", sessions: "things/devise/sessions"}

控制器:

class things::Devise::RegistrationsController < Devise::RegistrationsController
  def update
    # For Rails 4
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
    # For Rails 3
    # account_update_params = params[:user]

    # required for settings form to submit when password is left blank
    if account_update_params[:password].blank?
      account_update_params.delete("password")
      account_update_params.delete("password_confirmation")
    end

    @user = thing.find(current_thing.id)
    if @user.update_attributes(account_update_params)
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
       redirect_to thing_path(@user)
    else
      render "edit"
     end
  end

点击提交时会发生什么:

No route matches [PUT] "/things/edit"

耙路线:

               new_thing_session GET    /things/sign_in(.:format)                      things/devise/sessions#new
                  thing_session POST   /things/sign_in(.:format)                      things/devise/sessions#create
          destroy_thing_session DELETE /things/sign_out(.:format)                     things/devise/sessions#destroy
                 thing_password POST   /things/password(.:format)                     devise/passwords#create
             new_thing_password GET    /things/password/new(.:format)                 devise/passwords#new
            edit_thing_password GET    /things/password/edit(.:format)                devise/passwords#edit
                                 PATCH  /things/password(.:format)                     devise/passwords#update
                                 PUT    /things/password(.:format)                     devise/passwords#update
      cancel_thing_registration GET    /things/cancel(.:format)                       things/devise/registrations#cancel
             thing_registration POST   /things(.:format)                              things/devise/registrations#create
         new_thing_registration GET    /things/sign_up(.:format)                      things/devise/registrations#new
        edit_thing_registration GET    /things/edit(.:format)                         things/devise/registrations#edit
                                 PATCH  /things(.:format)                              things/devise/registrations#update
                                 PUT    /things(.:format)                              things/devise/registrations#update
                                 DELETE /things(.:format)                              things/devise/registrations#destroy

2 个答案:

答案 0 :(得分:1)

如果您希望能够从任何控制器渲染设计表单,您应该将它放在您的应用程序帮助器中:

 def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

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

希望这有帮助。

答案 1 :(得分:0)

您正在使用映射到表单中的GET请求的网址:edit_artist_registration_path。相反,您希望表单的网址指向艺术家注册的PUTPATCH网址:

form_tag(artist_registration_path, method: 'PUT', id: "my-awesome-dropzone.dropzone")