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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:04:42+00:00 2026-06-15T19:04:42+00:00

I have a form for creating a model (named Image) and the list for

  • 0

I have a form for creating a model (named Image) and the list for all entities of this model on the same view. The model is associated in a one-to-many relation with a second model called Deck (one Deck has many Images). The list in my view looks like this:

# image list    
<% @deck.images.each  do |img| %> # iterates also through invalidated model
    <%= link_to 'Delete', deck_image_path(@deck, img), 
           :confirm => 'Sure?', :method => :delete %>
<% end %>

My problem occurs when the validation for the @image object fails. The iteration iterates also through the invalid image object and gives an error for the next line saying there is no such path for that object.

I could check every object for .new_record?, but is this the preferred way? Or am I doing something absolutely wrong? I have nobody else seen with such a problem ;/

Update 1 – Controller

def create
    @deck = Deck.find(params[:id])
    @image = @deck.images.build(params[:image])

        respond_to do |format|
            if @image.save
              # redirect
              flash[:notice] = "Saved succesfully."
              format.html { redirect_to(@deck) }
            else
              # render
              format.html { 
                flash.now[:error] = "Could not be saved."
                render 'decks/show'
              }
        end
    end
end

Update 2 – More confusion

Well, ok I am absolutely confused now. Changing the second line in the create method from

# not working
@image = @deck.images.build(params[:image])

to

# working
@image = Image.new(params[:image])
@image.deck = @deck

seems to work without any errors. What is the difference?

Update 3 – View and error message

<h3>Upload Image</h3>
<%= form_for @image, :url => deck_images_path(@deck), :html => { :multipart => true } do |form| %>
<fieldset>
    <legend>Upload new image</legend>
    <%= render :partial => 'shared/model_errors', :locals => {:model => @image} %>
    <%= form.file_field :image %><br/>
    <%= form.submit "Upload", :class => 'btn btn-primary' %>
</fieldset>
<% end %>

<h3>Show all existing images</h3>
<% @deck.images.each  do |img| %>
    <%= image_tag img.image.url %>  
    <%= link_to 'Delete'.html_safe, deck_image_path(@deck, img) %>
<% end %>

The error message:

No route matches {:controller=>"images", :action=>"destroy", :deck_id=>#<Deck id: 1, 
name: "Test", description: "Test", created_at: "2012-12-10 20:44:22", 
updated_at: "2012-12-10 20:44:22">, :id=>#<Image id: nil, created_at: nil, 
updated_at: nil, image_file_name: nil, image_content_type: nil, 
image_file_size: nil, image_updated_at: nil, deck_id: 1>}
  • 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-15T19:04:43+00:00Added an answer on June 15, 2026 at 7:04 pm

    According to [1]: Build vs new in Rails 3 parent_model.children.build oder parent_model.children.new seems to create a relationship in memory instantly. So parent_model.children gets filled with a new children model having its parent_id = parent.

    The alternative with Children.new seems to add this new relationship after saving the new child object. Therefore its missing in an iteration over all children.

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

Sidebar

Related Questions

I have a jQuery Model windows which contains a form. On creating of the
I've creating one ASP web application, in that application one form have to update
I am creating one form in html using table. Which have one Drop down
I'm creating a simple form and in this form I want to have an
I have model Account, which has n, :transfers . In the list of all
Specifically, I have a form for creating a User . One of the fields
I'm creating a form based on a model, where I have a boolean field
I made one big form with many associations hasMany/HABTM. All is workin great on
I have a Django form wizard working nicely for creating content of one of
I'm creating a form. As of right now I have simple MAILTO form that

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.