简体   繁体   English

Rails 4-更新模型中的特定属性

[英]Rails 4 - Updating Specific Attributes in model

I'm building a marketplace app (rails 4) where sellers can list items to sell. 我正在构建一个市场应用程序(第4条),卖家可以在其中列出要出售的商品。 I have a seller profile form where users enter some details about their brand. 我有一个卖家资料表格,用户可以在其中输入有关其品牌的一些详细信息。

I'm not able to get the record to update with user inputs. 我无法使用用户输入来更新记录。 The form submits without any error but the model is not updated. 表单提交无任何错误,但模型未更新。

Code below. 下面的代码。 Demo at http://mktdemo.herokuapp.com/seller/18 (login: test@test.com / pwd: test1234) http://mktdemo.herokuapp.com/seller/18上进行演示(登录:test@test.com / pwd:test1234)

route: 路线:

match "/seller/:id", to: "users#sellerprofile", via: [:get, :put], as: :sellerprofile

my form: 我的表格:

<%= form_for(@user, url: sellerprofile_path(id: current_user.id), method: :put, html: { :class => 'edit_profile', :multipart => true }) do |f| %>

  <div class="form-group">
    <%= f.label :your_story %><i> (required)</i>
    <%= f.text_area :profilestory, class:"form-control" %>
  </div>

  <div class="form-group">
    <%= f.label :profile_image %><i> (required)</i>
    <%= f.file_field :profileimage, class:"form-control" %>
  </div>

   <div class="form-group">
        <%= f.submit class:"btn btn-primary" %>
   </div>

 <% end %>

user_controller: user_controller:

def sellerprofile
     @user = User.find(params[:id])
     @user.update_attributes(:profilestory => params[:profilestory], :profileimage => params[:profileimage])
end

For the image, I'm using paperclip and have the has_attached... code in my model. 对于图像,我使用回形针,并在模型中包含has_attached ...代码。

UPDATE: Here is my user_params in controller: 更新:这是我在控制器中的user_params:

  def user_params
    params.require(:user).permit(:bankaccname, :profileimage, :profilestory)
  end

When I use @user.update(user_params) in the sellerprofile method, I get a params :user not found error . 当我在Sellerprofile方法中使用@user.update(user_params)时,出现了params :user not found error This error occurs when I load the form (not on submit). 当我加载表单(不在提交时)时,会发生此错误。 Error is in the line params.require(:user) 错误在params.require(:user)

UPDATE 2: Here is the update method. 更新2:这是更新方法。 I'm using this for another form that takes in some bank account data so I'm not sure if I can modify this for the profile form. 我正在将其用于另一种需要一些银行帐户数据的表格,所以我不确定是否可以针对个人资料表格进行修改。

def update
    @user.attributes = user_params
    Stripe.api_key = ENV["STRIPE_API_KEY"]
      token = params[:stripeToken]

      recipient = Stripe::Recipient.create(
        :name => user_params["bankaccname"], 
        :type => "individual",              
        :bank_account => token              
        )                                   

      @user.recipient = recipient.id

     respond_to do |format|
      if @user.save
        format.html { redirect_to edit_user_url, notice: 'Your account was successfully updated.' }
      else
        format.html { render action: 'edit' }
      end
    end
  end

If you look at your question, it says: 如果您看您的问题,它说:

When I use @user.update(user_params) in the sellerprofile method, I get a params :user not found error. 当我在Sellerprofile方法中使用@ user.update(user_params)时,出现了params:user not found错误。 This error occurs when I load the form (not on submit) 当我加载表单(不在提交状态)时,会发生此错误

This line tells us that there is something wrong in the action where your form is rendered. 此行告诉我们呈现表单的操作存在问题。 I mean the url you hit in browsers address bar to display your form 我的意思是您在浏览器地址栏中点击以显示表单的网址

And in your comment you told me that the url for rendering your form is /seller/:id and now lets look at your form code 在您的评论中,您告诉我呈现表单的网址是/seller/:id ,现在让我们看一下表单代码

<%= form_for(@user, url: sellerprofile_path(id: current_user.id), method: :put, html: { :class => 'edit_profile', :multipart => true }) do |f| %>

Notice the url used in the form? 注意到表格中使用的网址了吗? You are using the same action to render your form and then after submitting form it'll again take you to same action which is wrong. 您正在使用相同的操作来呈现表单,然后在提交表单后再次将您带到相同的操作,这是错误的。 You should make two different routes. 您应该选择两条不同的路线。 One to render your form and other one to create it. 一种呈现您的表单,另一种呈现您的表单。 To explain it further, if you look at your code 进一步解释一下,如果您看一下代码

 def sellerprofile
   @user = User.find(params[:id])
   @user.update_attributes(user_params) 
 end

So when you hit /seller/:id it takes you to sellerprofile method and hence code inside your sellerprofile method fires up and it tries to update your user because of this line @user.update_attributes(user_params) but there are no user_params as you didn't submit any form so you get a params :user not found errorr 因此,当您命中/seller/:id 它将带您进入Sellerprofile方法,因此您的Sellerprofile方法中的代码将启动,并且由于此行@user.update_attributes(user_params)而尝试更新您的用户,但是没有user_params没有提交任何表格,所以您得到了一个参数:user not found errorr

Fix: 固定:

Just make two different routes, one to render your form and another one to create your record: 只需执行两种不同的方法,一种方法可以渲染表单,而另一种方法可以创建记录:

match "/seller/:id/new", to: "users#sellernew", via: [:get], as: :sellernewprofile
match "/seller/:id", to: "users#sellerprofile", via: [:put], as: :sellerprofile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM