2014-09-04 70 views
1

我正在構建一個市場應用程序,賣家可以列出要出售的物品。我使用設計進行身份驗證,並且還有用戶路由在註冊後收集用戶輸入。Rails 4 - 將用戶數據輸入模型

我試圖將此表格設置爲從用戶收集兩個賣家配置文件輸入並將其保存在用戶模型中。但控制器沒有讀取用戶,我得到一個錯誤param not found: user錯誤。

我的路線:匹配線添加了我發佈的個人資料表單。

devise_for :users 

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

    match "https://stackoverflow.com/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/ 

回答

0

路線

首先第一件事情:

#config/routes.rb 
devise_for :users 

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

這是一種更簡潔的方式來處理路線。

-

控制器

接下來,你需要處理的控制器的數據處理能力。要做到這一點,你需要確保你發送正確的PARAMS(我相信你如果你是不是會在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方法之間的區別?

如果你想在update方法使用Stripe細節,爲什麼不只是有條紋處理一個單獨的方法,並調用它,如果你已經設置一定PARAMS:

#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 
+0

夫婦的問題 - 1)時我根據你的建議改變了路線,我得到了'找不到沒有ID的用戶'錯誤。 2)如果我在響應中使用更新部分下的控制器代碼,是否需要更改sellerprofile方法?你在這裏回答了我的相關問題 - http://stackoverflow.com/questions/25657969/rails-4-updating-model-via-multiple-forms/25661012#25661012 – Moosa 2014-09-04 15:09:49