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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:17:44+00:00 2026-06-04T10:17:44+00:00

My application has two associated models: Magazine and Article: class Magazine < ActiveRecord::Base has_one

  • 0

My application has two associated models: Magazine and Article:

class Magazine < ActiveRecord::Base
  has_one :article
end

class Article < ActiveRecord::Base
  belongs_to :magazine
  validation_presence_of :title
end

From the Magazine show page I can create a new Article, so my routes.rb is configured like:

resources :magazines, :shallow => true do
  resources :articles
end

and in the Magazine show page I have the link “New article”, like:

<%= link_to 'New article', new_magazine_article_path(@article)

and an article helper to pass correct parameters to the form_for:

module ArticlesHelper
  def form_for_params
    if action_name == 'edit'
      [@article]
    elsif action_name == 'new'
      [@magazine, @article]
    end
  end
end

so I can use Article form_for like:

<%= simple_form_for(form_for_params) do |f| %> ...

The ArticlesController methods for new and create are:

respond_to :html, :xml, :js

def new
  @magazine = Magazine.find(params[:magazine_id])
  @article = Article.new
end

def create
  @magazine = Magazine.find(params[:magazine_id])
  @article = @magazine.build_article(params[:article])        
  if @article.save
    respond_with @magazine # redirect to Magazine show page
  else
    flash[:notice] = "Warning! Correct the title field."
    render :action => :new
  end
end

The problem occurs when there is a validation error with the title attribute, and the action new is rendered. In this moment I get the message: undefined method `model_name’ for NilClass:Class in the first line of form_for. I think it is because the @magazine parameter passed in the helper.

How could I solve this problem withou use redirect_to ? (I want to mantain the other attributes that were filled in the form .)

  • 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-04T10:17:45+00:00Added an answer on June 4, 2026 at 10:17 am

    Your form_for_params method is returning nil, because the action_name is set to ‘create’, not ‘new’ or ‘edit’.

    Try this:

    elseif action_name == 'new' or action_name == 'create'
      [@magazine, @article]
    
    • 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 in my Rails application: class Shift < ActiveRecord::Base has_many
My rails application has two models, gallery and photo. Galleries have many photos and
I have a Rails simple application that has two main models. A person model
My application has two node types: a parent node which can hold recursive child
I have an application that has two threads. The first one (the main thread)
I'm trying to build an iPhone application that has two subviews in the main
I would like to create a UIBarButtonItem on my iPhone application which has two
I am working on a simple app tab-bar based application that has two views.
I am building a web application using ASP.NET MVC that has two very distinct
In my tennis application, my 'match' table has two players (obviously). I've used player1_id

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.