我正在構建一個市場應用程序,賣家可以列出要出售的物品。我使用設計進行身份驗證,並且還有用戶路由在註冊後收集用戶輸入。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/
夫婦的問題 - 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