Rails 4 - 将用户数据输入模型

时间:2014-09-04 00:17:11

标签: ruby-on-rails

我正在建立一个市场应用程序,卖家可以列出要出售的商品。我使用设计进行身份验证,并在注册后使用用户路由收集用户输入。

我正在尝试设置此表单以从用户收集两个卖家配置文件输入并将其保存在用户模型中。但是控制器没有读取用户我收到错误param not found: user错误。

我的路线:为我发布的个人资料表单添加了匹配行。

  devise_for :users

  resources :users, only: [:update, :edit]

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

我的用户控制器方法:请注意我正在使用其他表单的更新过程,所以我认为我不能将它用于此表单。

def sellerprofile
    @user = User.find(params[:id])
   # if params[:user]
      respond_to do |format|
        if @user.update(user_params)
           format.html { redirect_to root_url, notice: 'Your profile was successfully updated.' }
        else
          format.html { render action: 'edit' }
        end
      end
    #end
end

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

      recipient = Stripe::Recipient.create(
        :name => user_params["bankaccname"], #if i want to save to db, use this: @user.bankaccname. not saving it                 
        :type => "individual",              #gives you only one set of error messages. i don't need the respond
        :bank_account => token              #to block either i think. eg. when i enter firstname only, i get two error
        )                                   #msgs

      @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

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

sellerprofile.html.erb

<%= form_for @user, url: sellerprofile_path(@user), html: { method: :put, :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.rb:

has_attached_file :profileimage, 
                  :styles => { :profile => "200x200", :p_thumb => "100x100>" },
                  :default_url => ""
validates_attachment_content_type :profileimage, :content_type => /\Aimage\/.*\Z/

1 个答案:

答案 0 :(得分:0)

<强>路线

首先要做的事情:

#config/routes.rb
devise_for :users

resources :users, only: [:update, :edit] do
  match :sellerprofile, via: [:get, :put], as: :sellerprofile
end

这是一种更清洁的方式来处理路线。

-

<强>控制器

接下来,您需要处理控制器的数据处理功能。要做到这一点,你需要确保你发送了正确的参数(我相信你是。如果你不是,它将在url中):

<%= form_for @user, url: sellerprofile_path, method: :put, html: { multipart: true } do |f| %>

您可能希望在Rails中观察此路由结构:

enter image description here

这是Rails中的标准路由结构(从resources指令调用)。这意味着如果你想使用相当于update的路线,你可能会省略@user变量(虽然我没有测试过)

-

为了使您的工作正常,您需要确保按以下方式传递参数:

params => {
  "user" => {
     "id" => "1",
     "other" => "value", 
  }

这将由form_for方法处理。当您在控制器中调用user_params时,params.require块会查找user参数; permit块查找其中包含的各个值。

这将允许您执行以下操作:

#app/controllers/users_controller.rb
def UsersController < ApplicationController
   def sellerprofile
      @user = User.find params[:id]
      @user.update user_params
   end

   private

   def user_params
      params.require(:user).permit(:x, :y, :z)
   end
end

这应该可以根据需要使用

-

<强>更新

就个人而言,我没有看到你在这里做什么和使用update方法之间的区别?

如果要在Stripe方法中使用update详细信息,为什么不只是为条带处理设置单独的方法,如果设置了某些参数,则调用它:

#app/controllers/users_controller.rb
class UsersController < ApplicationController
  def update
    @user.attributes = user_params
    Stripe.api_key = ENV["STRIPE_API_KEY"]
    token = params[:stripeToken]

    if token. present?
      recipient = stripe
      @user.recipient = recipient.id
    end

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

  private

  def stripe
     return Stripe::Recipient.create(
        :name => user_params["bankaccname"], #if i want to save to db, use this: @user.bankaccname. not saving it                 
        :type => "individual",              #gives you only one set of error messages. i don't need the respond
        :bank_account => token              #to block either i think. eg. when i enter firstname only, i get two error
        )     
  end

  def user_params
      params.require(:user).permit(:x, :y, :z)
  end
end