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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:18:29+00:00 2026-06-03T01:18:29+00:00

I have 2 models (Book & Image) class Book < ActiveRecord::Base has_many :images accepts_nested_attributes_for

  • 0

I have 2 models (Book & Image)

class Book < ActiveRecord::Base

  has_many :images

  accepts_nested_attributes_for :images

end 




class Image < ActiveRecord::Base

    belongs_to :book

    has_attached_file :data, :path => ":rails_root/public/system/datas/:book_id/:style/:basename.:extension",
                      :url => "/system/datas/:book_id/:style/:basename.:extension",


    :styles => {
      :thumb => ["150x172#",:jpg],
      :large => ["100%", :jpg]
    }

  validates_attachment_presence :data
    validates_attachment_content_type :data,
    :content_type => ['image/jpeg', 'image/pjpeg',
                    'image/jpg', 'image/png', 'image/tiff', 'image/gif'], :message => "has to be in a proper format"



end

I would like to modify my application so that when users create a new book and uploads an image, they’re redirected to a specific page, otherwise they are directed to the ‘Show’ page

I have modified my ‘create’ action in the Book Controller as follows:

def create
    @book = Book.new(params[:book])


    if @book.save

      flash[:notice] = 'Book was successfully created'

      if params[:book][:image][:data].blank?  # === this line is giving me errors
        redirect_to @specific_page
      else  
         redirect_to show_book_path(@book.id)
      end

    else
      render :action => 'new'
    end

  end

I want to test that if an image or multiple images have been uploaded, user should be directed to a different page

The following if condition is erroneous:

if params[:book][:image][:data].blank?  # === this line is giving me errors

I would appreciate it if someone could suggest me how to to check if images have been attached to the new Book.

Please note that I want to check for only newly uploaded images, not all images associated with the book.

Thanks

  • 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-03T01:18:31+00:00Added an answer on June 3, 2026 at 1:18 am

    First of all, I am not sure about your view file. But if user has not selected/uploaded a single image, then params[:book][:image] will be nil and thus ‘if params[:book][:image][:data].blank?’ line give error.

    Try checking the ‘image’ instances associated with book instance

    if @book.images.any?
      redirect_to @specific_page
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models class Book < ActiveRecord::Base has_many :chapters end and class
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have 2 models: Video: class Video < ActiveRecord::Base belongs_to :user has_many :thumbnails attr_accessor
I have two models with slug fields: class Book(models.Model): name = models.CharField(max_length=200) slug =
I have a simple Book Author relationship class Author(models.Model): first_name = models.CharField(max_length=125) last_name =
I have 03 models book->target_readers and book has collection. class Book(models.Model): def __unicode__(self): return
In Django I have this: models.py class Book(models.Model): isbn = models.CharField(max_length=16, db_index=True) title =
I have models as follows: class Book(models.Model): title = models.CharField(max_length=400) def transactions_all_time(self): latest_transactions =
I have the following models class Book(models.Model): name = models.CharField(max_length=140) class UserProfile(models.Model): favorites =
I have the following models: class Author(models.Model): author_name = models.CharField() class Book(models.Model): book_name =

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.