2010-09-01 85 views
0

好吧,所以我遇到了在rails中執行多個表單的問題。這裏是低於Rails Multiform結構似乎有點關閉

型號代碼

class Profile < ActiveRecord::Base 
    belongs_to :user 
    has_attached_file :avatar, :styles => { :medium => "134x137>", :thumb => "111x111>", :tiny => "32x38>" } 
    validates_attachment_content_type :avatar, :content_type => ['image/pjpeg','image/jpeg', 'image/x-png', 'image/png', 'image/gif'] 


class User < ActiveRecord::Base 
    has_one :profile, :dependent => :destroy 

檔案控制器

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

配置文件,編輯,查看

<% form_for @user do |f| %> 
<%= f.text_field :first_name %> 
<%= f.text_field :last_name %> 
<%= f.text_field :email %> 
<%= f.password_field :password %> 
<%= f.password_field :password_confirmation %> 
<input id="send_update" name="send" type="submit" value="Update" /> 
<% end %> 


<% form_for @profile , :html => { :multipart => true } do |f| %> 
<%= render :partial => 'form', :locals => {:f => f, :profile => @profile, :title => 'Edit Profile'} %> 
<%= submit_tag 'Update', :style => 'text_align:right'%> 
<% end %> 

檔案_form部分

<label>Upload Avatar</label> 
<tr><%= f.file_field :avatar %></tr> 

所以基本上我在編輯視圖兩種形式,當我點擊第二個更新更新化身,我去給用戶的更新,我得到這個閃存錯誤「抱歉,出事了」

def update 
    @user = User.find(params[:id])  
    current_email = @user.email 
if @user.update_attributes(params[:user]) 
    UserMailer.deliver_email_changed (@user) if email_changed?(current_email, @user.email) 
    flash[:notice] = "<h1>Account updated!</h1>" 
    redirect_to edit_user_path(@user) 
else 
    flash.now[:error] = "Sorry, something went wrong" 
    render :action => :edit 
end 
end 

我的問題是這樣

  1. 有沒有更好的這種結構的方式,所以也許我有一種形式?
  2. 爲什麼現在不存儲和什麼導致問題?

回答

0

您使用更新的方法是錯誤的,它在語法上是無效的(你缺少一個end)。它應該是:

def update 
    @user = User.find(params[:id])  
    current_email = @user.email 
    if @user.update_attributes(params[:user]) 
    UserMailer.deliver_email_changed (@user) if email_changed?(current_email, @user.email) 
    flash[:notice] = "<h1>Account updated!</h1>" 
    redirect_to edit_user_path(@user) 
    else 
    flash.now[:error] = "Sorry, something went wrong" 
    render :action => :edit 
    end 
end 

它應該是確實兩種形式,因爲我猜你不希望任何一種形式的值,如果用戶執行其他操作來提交。

現在,組織你的控制器。您在您的ProfilesController上撥打@user = User.find(params[:id]),但您傳遞的ID是用戶的ID。這應該在用戶的控制器上,並從那裏更新關聯的配置文件,或者您應該接收配置文件對象的ID。

我會與第一個去。您可以更新使用accepts_nested_attributes_for用戶的配置文件對象,你的表格會是這樣:

<% form_for @user do |f| %> 
    <%= f.text_field :first_name %> 
    <%= f.text_field :last_name %> 
    <%= f.text_field :email %> 
    <%= f.password_field :password %> 
    <%= f.password_field :password_confirmation %> 
    <%= f.submit, :id => ... %> 
<% end %> 

<% form_for @user, :html => { :multipart => true } do |f| %> 
    <% f.fields_for :profile do |profile_form| %> 
    <%= render :partial => 'form', :locals => {:f => profile_form, :title => 'Edit Profile'} %> 
    <%= submit_tag 'Update', :style => 'text_align:right'%> 
    <% end %> 
<% end % 

如果錯誤的密碼不能爲空,可能是由於validates_presence_of :password, :password_confirmation。你應該使用conditional validation那裏

0

故障排除將幫助:

  1. 取出的if/else,並保持@ user.update_attributes(PARAMS [:用戶])。 Rails會給你一個更詳細的錯誤信息。
  2. 檢查表單結構(html源代碼),尤其是字段命名。
  3. 檢查日誌文件的數據庫語句

HTH

+0

錯誤是密碼不能爲空,但我只是更新頭像 – Trace 2010-09-01 13:41:37