创建form_for用于贡献用户图像和描述 [英] Creating form_for for atributes User image and description

查看:192
本文介绍了创建form_for用于贡献用户图像和描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a rails application with devise, and I added to users a profile image, and a description. What I want to do is to create a page (DIFFERENT of the default registration/edit) where the users, after logged in, can set only this two atributes(image and description).

        <%= form_for(:user, html: { method: :put, :multipart => true })) do |f| %>
         <div class="form-group">
           <%= f.label :Profile_Image %>
           <%= f.file_field :image, class: "form-control" %>
         </div>
         <div class="form-group"> 
          <%= f.label :Descrição %>
            <%= f.text_area :description, class: "form-control", rows: "10" %>
         </div>
        <% end %>

我已经尝试过两个不同的控制器,没有一个工作:

I have already tried two different controllers and none of them worked:

def edit
end

def edit
  @user = User.find(params[:id])
end

我的配置 - >路由是:

My config -> routes are:

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

但是当我点击提交它什么都不做!我是新的在rails,我正在努力想出几个小时...如何创建页面只更新图像和描述?感谢

But when i click submit it does nothing! I am new at rails and I am trying to figure this out for hours... How can I create a page to update only the image and the description? Thanks

推荐答案

您需要在控制器中使用更新方法。您的编辑方法允许表单呈现,但您需要这样:

You need an update method in your controller. Your edit method allows the form to render, but you need something like this:

def update
   current_user.update(user_params)
end

那么你的控制器中会有另一个方法叫user_params,看起来像这样。我被教导把它放在一个私人标题下。

Then you would have another method in your controller called user_params, which would look something like this. I was taught to put it under a private heading.

private
   def user_params
     params.require(:user).permit(:profile_image, :description)
   end

我相信有一种使用你的更新方法来包含你的参数的快捷方式,但是这样做可以。

I believe there is a shortcut way of including your params with your update method, but this will do.

这篇关于创建form_for用于贡献用户图像和描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆