开发者

Rails Multiform structure seems a bit off

开发者 https://www.devze.com 2023-01-14 22:13 出处:网络
Ok so I am having a problem with doing multiple forms in rails. here is the code below Models class Profile < ActiveRecord::Base

Ok so I am having a problem with doing multiple forms in rails. here is the code below

Models

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

Profile Controller

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

Profiles Edit View

<% 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 %>

Profile _form partial

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

So basically I have two forms in t开发者_如何学Gohe edit view and when i click on the second Update to update the avatar I go to the users update and i get this flash error "Sorry, something went wrong"

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

My questions are this

  1. Is there a better way to structure this so maybe i have one form?
  2. Why is it not saving now and whats causing the issue?


The method you are using to update is wrong, it is syntactically invalid (you're missing an end). It should be:

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

It should be two forms indeed, as I'm guessing you don't want any of the values of one form to submit if the user performs the other action.

Now, organize your controllers. You are calling @user = User.find(params[:id]) on your ProfilesController, but the id you're passing is an user's one. Either this should be on the user's controller, and update the associated profile from there, or you should receive the id of the profile object instead.

I'd go with the first one. You can update the Profile object of a user using accepts_nested_attributes_for, and your forms would be like:

<% 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 %

If the error is that the password cannot be blank, may be due to a validates_presence_of :password, :password_confirmation. You should use a conditional validation there


Troubleshooting would help:

  1. Remove the if/else and keep the @user.update_attributes(params[:user]). Rails would give you a more detailed error message.
  2. Check the form structure (html source) especially for field naming.
  3. Check the log files for DB statements

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消