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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:25:48+00:00 2026-05-22T18:25:48+00:00

In my rails application I have two associated models called Magazine and Article. Magazine

  • 0

In my rails application I have two associated models called Magazine and Article.

Magazine

class Magazine < ActiveRecord::Base
  has_many :articles
end

Article

class Article < ActiveRecord::Base
  belongs_to :magazine
end

Routes

Magazineapp::Application.routes.draw do
  resources :magazines do
    resources :articles
  end
end

schema.rb

create_table "articles", :force => true do |t|
  t.string   "title"
  t.string   "author"
  t.integer  "magazine_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "magazines", :force => true do |t|
  t.string   "title"
  t.datetime "created_at"
  t.datetime "updated_at"
end

I am trying to create a new article associated to a magazine from the article’s new page. So, I created a link in the magazine’s show page to pass the selected magazine to the new article’s page.

views/magazines/show.html.erb

<p id="notice"><%= notice %></p>
<p>
  <b>Title:</b>
  <%= @magazine.title %>
</p>

<%= link_to 'Edit', edit_magazine_path(@magazine) %> |
<%= link_to 'Back', magazines_path %> |
<%= link_to 'New Article', new_magazine_article_path(@magazine) %>

The partial that is rendered in the article’s new page is:

views/articles/_form.html.erb

<%= simple_form_for([@magazine, @article]) do |f| %>
  <%= f.error_notification %>

  <div class="inputs">
    <%= f.input :title %>
    <%= f.input :author %>
  </div>

  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>

And the create method from the article’s controller is:

def create

  @magazine = Magazine.find(params[:magazine_id])    
  @article = @magazine.articles.create(params[:article])

  respond_to do |format|
    if @article.save
      format.html { redirect_to(@article, :notice => 'Article was successfully created.') }
      format.xml  { render :xml => @article, :status => :created, :location => @article }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
    end
  end
end

But when I try to create a new article associated to the magazine I get the error message:

Showing /home/wilson/magazineapp/app/views/articles/_form.html.erb where line #1 raised:

undefined method `articles_path' for #<#<Class:0x00000001944070>:0x00000001926ed0>
Extracted source (around line #1):

1: <%= simple_form_for([@magazine, @article]) do |f| %>
2:   <%= f.error_notification %>
3: 
4:   <div class="inputs">

Request

Parameters:

{"magazine_id"=>"2"}

rake routes

    magazine_articles GET    /magazines/:magazine_id/articles(.:format)          {:action=>"index", :controller=>"articles"}
                      POST   /magazines/:magazine_id/articles(.:format)          {:action=>"create", :controller=>"articles"}
 new_magazine_article GET    /magazines/:magazine_id/articles/new(.:format)      {:action=>"new", :controller=>"articles"}
edit_magazine_article GET    /magazines/:magazine_id/articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
     magazine_article GET    /magazines/:magazine_id/articles/:id(.:format)      {:action=>"show", :controller=>"articles"}
                      PUT    /magazines/:magazine_id/articles/:id(.:format)      {:action=>"update", :controller=>"articles"}
                      DELETE /magazines/:magazine_id/articles/:id(.:format)      {:action=>"destroy", :controller=>"articles"}
            magazines GET    /magazines(.:format)                                {:action=>"index", :controller=>"magazines"}
                      POST   /magazines(.:format)                                {:action=>"create", :controller=>"magazines"}
         new_magazine GET    /magazines/new(.:format)                            {:action=>"new", :controller=>"magazines"}
        edit_magazine GET    /magazines/:id/edit(.:format)                       {:action=>"edit", :controller=>"magazines"}
             magazine GET    /magazines/:id(.:format)                            {:action=>"show", :controller=>"magazines"}
                      PUT    /magazines/:id(.:format)                            {:action=>"update", :controller=>"magazines"}
                      DELETE /magazines/:id(.:format)                            {:action=>"destroy", :controller=>"magazines"}

How can I solve this problem? What are the correct parameters to pass to the simple_form_for in this case?

  • 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-22T18:25:49+00:00Added an answer on May 22, 2026 at 6:25 pm

    When you have nested resources you have to assure that you always pass the higher level resource between your requests.

    Your routes will be the ones you have noticed using rake routes.

    The form should be like this:

    <%= simple_form_for([@magazine, @article]) do |f| %>
      <%= f.error_notification %>
    
      <div class="inputs">
        <%= f.input :title %>
        <%= f.input :author %>
      </div>    
      <div class="actions">
        <%= f.button :submit %>
      </div>
    <% end %>
    

    This way now you have to use the new routes everywhere on your articles controller and articles view pages, which will include the magazine itself.

    Your create action in the controller would be:

    def create
    
      @magazine = Magazine.find(params[:magazine_id])    
      @article = @magazine.articles.create(params[:article])
    
      respond_to do |format|
        if @article.save
          format.html { redirect_to([@magazine,@article], :notice => 'Article was successfully created.') }
          format.xml  { render :xml => @article, :status => :created, :location => @article }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
        end
      end
    end
    

    So now all your routes should reference @magazine too.

    • 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
Suppose I have two classes in a Rails application: class Subject < ActiveRecord::Base def
I have two models in my rails application Class Employee belongs_to :cost_center End Class
In my rails application I have two models called Kases and Notes. They work
I have two models/tables in my Rails application: discussions and comments. Each discussion has_many
In my Rails application, I have two models, Articles and Projects, which are both
In rails application I have two models: Food and Drink . Both food and
I have a Rails application, with two models: SalesTransactions and PurchaseOrders. In the PurchaseOrders
I am developing a rails application in which I have two models User and
I have a Rails simple application that has two main models. A person model

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.