Damir Nurgaliev
Damir Nurgaliev

Reputation: 351

Simple form for params

I have 2 models, blogs and posts

  resources :blogs do
    resources :posts
  end

So, also i have an association. One blog can have a lot of posts. So I put the link to new post in the index of blog:

= link_to 'New Post', new_blog_post_path(@blog)

And then it redirects you to new post, which renders a form like this:

  = simple_form_for(@post) do |f|
  = f.error_notification

  .form-inputs
    = f.input :title
    = f.input :content

  .form-actions
    = f.button :submit

And I'm getting an error:

undefined method `posts_path'

I think simple form requires the blog_id but I could't find it. I tried to put @blog.id to simple form, but anyway i got an error (id for nil class) How can I solve my problem?

Upvotes: 0

Views: 241

Answers (1)

James Milani
James Milani

Reputation: 1943

I've never used simple_form_for, but it looks to me like your resource has association that is assigned. So you'll need to include it in the form definition:

= simple_form_for([@blog, @post]) do |f|
= f.error_notification

.form-inputs
  = f.input :title
  = f.input :content

.form-actions
  = f.button :submit

And obviously you'll need to have @blog available in your controller action.

Link to the form_for docs: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

EDIT:

Not sure where posts_path is coming from in your code, but obviously that route doesn't exist as all post paths are dependent on blogs.

Is there anywhere where you reference a post_path in your code?

Upvotes: 1

Related Questions