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 7932471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:01:53+00:00 2026-06-03T21:01:53+00:00

I have a system setup where users can post microposts (basically status updates) and

  • 0

I have a system setup where users can post microposts (basically status updates) and upload a photo to go with the micropost.

I have:

Micropost model (has_one photo)
Photo_album model (has_many photos)
Photo model (belongs_to micropost, belongs_to photo_album)

User fills in text in text area and selects a photo. Upon submission the microposts table is updated with micropost related data such as content, created_at etc.

At the same time I want my photos table (Photo model) updated with the photo selection the user made but the correct photo album. If you look below you can see in my users_controller the instance variable @photo’s value. This insures that the photo uploaded is linked to the correct photo album which is named “microposts album”. It’s purpose is to link up to all micropost related photos.

I have a Users_controller:

  def new
     @user = User.new 
     @micropost = Micropost.new(:user_id => users_id)
     @photo = Photo.new(:photo_album_id => PhotoAlbum.where(:user_id => current_user.id, :album_title => "microposts album").first.id)
  end

From a previous question I asked it was established I needed to use accepts_nested_attributes_for, fields_for in order to be able to update more than one model with one form. This is how I’ve set things up.

Micropost model:

class Micropost < ActiveRecord::Base

  belongs_to :user
  has_one  :photo
  accepts_nested_attributes_for :photo
  attr_accessible :content, :user_id, :poster_id, :username, :image, :remote_image_url

end

Photo model:

class Photo < ActiveRecord::Base

  belongs_to :photo_album

    attr_accessible :photo_album_id, :photo_title, :image, :remote_image_url
    mount_uploader :image, ImageUploader

end

Finally here is the micropost form:

= form_for @micropost, :remote => true do |f|
    = f.fields_for @photo do |p|
        = p.file_field :image
    = f.hidden_field :user_id
    = f.text_area :content
    = f.submit "Post"

At first I got the error:

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: photo):

I was slightly confused because I thought the attributes were assigned automatically. At lesast thats what I read in the docs anyway. Anyway I went ahead and added :photo to the micropost model attr_accessible white list.

That first error went then I got this one:

ActiveRecord::AssociationTypeMismatch (Photo(#2169424320) expected, got ActiveSupport::HashWithIndifferentAccess(#2157396720)):

Maybe I’ve misunderstood how this feature works but I’ve read through this and also looked at the 3.2.3 api doc but not where I’m going wrong.

I would really appreciate some help with getting this to work.

Thanks in advance and I hope the long post was off putting. Just thought providing all this info would make people understand what I’m trying to do better.

Kind regards

Update:
Using :photo_attributes instead of photos gives me the following error:

Started POST "/microposts" for 127.0.0.1 at 2012-05-10 21:01:11 +0100
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1] Processing by MicropostsController#create as JS
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1]   Parameters: {"micropost"=>{"photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000102c293d8 @original_filename="7seriesbmw.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"micropost[photo][image]\"; filename=\"7seriesbmw.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/fh/fhADKPjGG8qSuCeoHCTNYE+++TI/-Tmp-/RackMultipart20120510-14787-1e1mrhh>>}, "user_id"=>"2", "content"=>"ioo"}, "commit"=>"Post", "utf8"=>"✓", "authenticity_token"=>"/y8Lr+e7xgabt60GWxnMGvCtIi7IjqrYDoA84vAqYcE=", "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"}
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1]   User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1] Completed 500 Internal Server Error in 649ms
[02b23327ad83000f75c418d8739e7f49] [127.0.0.1] 
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: photo):
  app/controllers/microposts_controller.rb:9:in `create'

After changing the attr_accessor back to :photo instead of :photo_attributes:

Started POST "/microposts" for 127.0.0.1 at 2012-05-10 21:20:07 +0100
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1] Processing by MicropostsController#create as JS
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1]   Parameters: {"micropost"=>{"photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000102f8a3b0 @original_filename="7seriesbmw.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"micropost[photo][image]\"; filename=\"7seriesbmw.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/fh/fhADKPjGG8qSuCeoHCTNYE+++TI/-Tmp-/RackMultipart20120510-15197-9rt2xn>>}, "user_id"=>"2", "content"=>"pp"}, "commit"=>"Post", "utf8"=>"✓", "authenticity_token"=>"/y8Lr+e7xgabt60GWxnMGvCtIi7IjqrYDoA84vAqYcE=", "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01"}
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1]   User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1] Completed 500 Internal Server Error in 452ms
[985e0f204bf7ffac1f7c02fbec35ad9b] [127.0.0.1] 
ActiveRecord::AssociationTypeMismatch (Photo(#2180069640) expected, got ActiveSupport::HashWithIndifferentAccess(#2153916820)):
  app/controllers/microposts_controller.rb:9:in `create'

Microposts controller create action:

def create


    if params[:micropost][:user_id].to_i == current_user.id
     @micropost = current_user.microposts.build(params[:micropost])

     @comment = Comment.new(:user_id => current_user.id)
        respond_to do |format|
            if @micropost.save


            format.js   { render :post_on_springboard }

            end
        end
    else

    user = User.find_by_username(params[:micropost][:username])
    @micropost = user.microposts.build(params[:micropost])
    if @micropost.save

    UserMailer.new_wall_post_notification(user, current_user).deliver if user.email_notification == 1

    flash[:success] = "Micropost posted"
    redirect_to root_path+user.username
    else
    flash[:error] = "#{@micropost.errors.full_messages.first}"
    redirect_to root_path+user.username
    end

    end

end
  • 1 1 Answer
  • 0 Views
  • 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-06-03T21:01:54+00:00Added an answer on June 3, 2026 at 9:01 pm

    changing

    f.fields_for @photo 
    

    to

    f.fields_for :photo 
    

    and adding

    @micropost.build_photo(:photo_album_id => :photo_album_id => current_user.photo_albums.find_by_album_title("microposts album").id) 
    

    to my microposts_controller fixed my issue

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

Sidebar

Related Questions

I am creating a system where users can setup mailings to go out at
i have a private section where specific users can login on my system (restricted
I am trying to setup a system where users can submit an equation to
I have a credit system set up on my site where user A can
I have a user system set up in a 'users' table, and I have
We have a ftp system setup to monitor/download from remote ftp servers that are
I have been asked to setup a course leaflet system for a college. For
Visual Studio Setup project: I have to check certain system requirements before my setup
I'm working on a survey system for my company and I have it setup
I have a function that updates a user in the asp.net membership provider. <AcceptVerbs(HttpVerbs.Post)>

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.