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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:19:21+00:00 2026-05-24T21:19:21+00:00

I have three models: class Country < ActiveRecord::Base has_many :regions has_many :assets, :dependent =>

  • 0

I have three models:

class Country < ActiveRecord::Base
  has_many :regions
  has_many :assets, :dependent => :destroy
  accepts_nested_attributes_for :assets
end


class Region < ActiveRecord::Base
  belongs_to :country
  has_many :appartments

  has_many :assets, :dependent => :destroy
  accepts_nested_attributes_for :assets
end


class Asset < ActiveRecord::Base
  belongs_to :region
  belongs_to :country
  has_attached_file :image,
    :styles => {
      :thumb=> "100x100>",
      :small  => "300x300>",
      :large => "600x600>"
  } 
end

Can some explain to me what the edit and new method/action is of the region controller, to store the asset(image)?

  • 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-05-24T21:19:22+00:00Added an answer on May 24, 2026 at 9:19 pm

    Here is an example of how you could do it by building your form in a certain way. It should work for both the new and update actions.

    <%= form_for @region do |f| %>
      <%= f.fields_for :assets, @region.assets.build do |fa| %>
        <%= fa.file_field :image %>
      <% end %>
    <% end %>
    

    What this does is to first create the form for the @region instance. And since you have the accepts_nested_attributes_for :assets you can use the fields_for method to work with the associations. The symbol :assets tells it which association to read from, but since you want to add a new image and not work with the existing, you can add the second argument which is a single new Asset instance created by the build method.

    And then finally you can add the file_field to actually upload the file.

    If you build your form this way you should not have to alter the action code inside your controller at all.

    Edit:

    And here is how the new and create actions could look like to make the view work:

    def new
      @region = Region.new
      # Add respond_to or respond_with if you want
    end
    
    def create
      @region = Region.new(params[:region])
      if @region.save
        render :action => :show
      else
        render :action => :new
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models : class LevelTwoArea < ActiveRecord::Base has_many :places, dependent: :restrict end
I have a three models: class Feed < ActiveRecord::Base has_many :filters, :dependent => :destroy
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have three models: class Tenant < ActiveRecord::Base has_many :sites end class Site <
I have three models defined as follows: class Comic < ActiveRecord::Base has_many :feeds end
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have three Models setup with the following associations class User < ActiveRecord::Base has_many
Three models: class Customer < ActiveRecord::Base has_many :visits end class Visit < ActiveRecord::Base belongs_to

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.