Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1030615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:43:44+00:00 2026-05-16T12:43:44+00:00

Ok so I am having a problem with doing multiple forms in rails. here

  • 0

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 the 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?
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:43:45+00:00Added an answer on May 16, 2026 at 12:43 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am a new user to Python and I am having problem doing
I'm having a problem with doing a sendSynchronousRequest failing. It only fails after I
I'm having a problem setting a cookie and doing a 302 redirect In chrome
I am having a problem on my local machine when I am doing testing.
Im doing a page in .asp and im having this problem ... I have
I'm having a bizarre problem and I'm not sure if I'm doing something wrong
I am having a little problem with Relative Layouts. I'm doing a project in
This is a seemingly simple problem but I am having trouble doing it in
I'm doing maintenance work on an existing Rails site and am having some problems
I'm using http://raphaeljs.com/ to try and draw multiple small circles. The problem I'm having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.