2010-09-01 88 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