Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为atributes用户图像和描述创建表单_Ruby On Rails_Devise - Fatal编程技术网

Ruby on rails 为atributes用户图像和描述创建表单

Ruby on rails 为atributes用户图像和描述创建表单,ruby-on-rails,devise,Ruby On Rails,Devise,我有一个带有Desive的rails应用程序,我向用户添加了一个配置文件图像和描述。我想做的是创建一个页面(不同于默认的注册/编辑),用户在登录后只能设置这两个属性(图像和描述) 我的配置->路线是: get "edit" => "pages#edit" post "edit" => "pages#edit" 但当我点击提交时,它什么也不做!我是rails的新手,我花了好几个小时试图解决这个问题。。。如何创建仅更新图像和描述的页面?谢谢您需要控制器中的更新方法。您的编辑方法允许表单

我有一个带有Desive的rails应用程序,我向用户添加了一个配置文件图像和描述。我想做的是创建一个页面(不同于默认的注册/编辑),用户在登录后只能设置这两个属性(图像和描述)

我的配置->路线是:

get "edit" => "pages#edit"
post "edit" => "pages#edit"

但当我点击提交时,它什么也不做!我是rails的新手,我花了好几个小时试图解决这个问题。。。如何创建仅更新图像和描述的页面?谢谢

您需要控制器中的更新方法。您的编辑方法允许表单进行渲染,但您需要以下内容:

def update
   current_user.update(user_params)
end
然后,您的控制器中还有另一个名为user_params的方法,看起来像这样。我被教导把它放在私人标题下

private
   def user_params
     params.require(:user).permit(:profile_image, :description)
   end
我相信有一种快捷方式可以将参数包含在更新方法中,但这样就可以了

  • 使用注册控制器设计,您应该自定义它
  • 您应该在一个控制器中有一个同名的方法,您有两个编辑方法。将一个编辑方法更改为更新方法(参考:)

    pages\u controller.rb

    class PagesController < Devise::RegistrationsController
    
    def edit
     @user = current_user
    end
    
    def update
     @user = current_user
    
     successfully_updated = if needs_password?(@user, params)
       @user.update_with_password(devise_parameter_sanitizer.for(:account_update))
     else
       params[:user].delete(:current_password)
       @user.update_with_password(devise_parameter_sanitizer.for(:account_update))
     end
    
     if successfully_updated
      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
    
    private
    def needs_password?(user, params)
      user.email != params[:user][:email] || params[:user][:password].present?
    end
    end
    
    class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception
    
    before_filter :configure_permitted_parameters, if: :devise_controller?
    
    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:account_update) do |u|
        u.permit(:description, :image, :email, :password, :password_confirmation)
      end
    end
    end
    
  • 在您的视图上看起来像(示例,未测试)

    update_pages_path,:html=>{:method=>:put})do | f |%>
    

  • 你能给我们看看你的控制器和你运行的rails版本吗?当你点击提交表单时,也可以使用stacktrace?我的rails版本是4!它不起作用,我试图按照设计教程,但它也不起作用,我所需要的正是它,我想让用户更新没有密码,但我遵循所有这些步骤,什么也没有。。。“当前PSAWORD不能为空”继续…“您能解释一下如何按照链接的教程进行操作吗?更新的答案,您能解释一下如何按照链接的教程进行操作吗?我想教程已经解释了你想要什么
    class PagesController < Devise::RegistrationsController
    
    def edit
     @user = current_user
    end
    
    def update
     @user = current_user
    
     successfully_updated = if needs_password?(@user, params)
       @user.update_with_password(devise_parameter_sanitizer.for(:account_update))
     else
       params[:user].delete(:current_password)
       @user.update_with_password(devise_parameter_sanitizer.for(:account_update))
     end
    
     if successfully_updated
      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
    
    private
    def needs_password?(user, params)
      user.email != params[:user][:email] || params[:user][:password].present?
    end
    end
    
    class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception
    
    before_filter :configure_permitted_parameters, if: :devise_controller?
    
    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:account_update) do |u|
        u.permit(:description, :image, :email, :password, :password_confirmation)
      end
    end
    end
    
    devise_scope :user do
     get "edit" => "pages#edit"
     put "update" => "pages#update"
    end
    
    <%= form_for(@user, :url => update_pages_path, :html => { :method => :put }) do |f| %>
      <%= devise_error_messages! %>
      <div class="form-group">
        <%= f.label :image, "Profile Image" %>
        <%= f.file_field :image, class: "form-control" %>
      </div>
      <div class="form-group"> 
        <%= f.label description, "Descrição" %>
        <%= f.text_area :description, class: "form-control", rows: "10" %>
      </div>
     <% end %>
    <%= f.submit "Save Image" %>