Reputation: 614
Note: Rails newb here.
So, I recently created a Rails app with mongoid
gem for use of MongoDB. I have a namespace route of :blog
with a nest of resource of posts
Routes.rb:
Rails.application.routes.draw do
namespace :blog do
resources :posts
end
end
The error comes from app/controllers/blog/posts_controller.rb:
Class Blog::PostsController < ApplicationController
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
I also have a 'post' model that comes with a title and body:
Class Post
include Mongoid::Document
field :title, type: String
field :body, type: String
end
In new.html.erb:
<h1>Rails Sample Blog</h1>
<%= form_for @post, url: blog_post_path do |f| %>
<div><%= f.label :title %><%= f.text_field :title %></div>
<div><%= f.label :body %><%= f.text_area :body %></div>
<% end %>
Is there something I left out that I didn't catch? It's slowly haunting me.
Edit: See this Git repo: https://github.com/hackathons-com/Rails-MongoDB-Devise-test
Edit 2:
undefined method `post_url' for #<Blog::PostsController:0x007f3d19105ee8>
Upvotes: 0
Views: 145
Reputation: 26
Try this:
def create
@blog = Blog.find(params[:blog_id])
@post= @blog.posts.build(post_params)
(This assumes that you already added a column of blog_id
and have your associations correct, although looking at your model it looks like you may have missing associations?)
Upvotes: 1
Reputation: 1289
app/controllers/blog/post_controller.rb:
Shouldn't this be posts_controller.rb?
Also
Class Blog::PostController < ApplicationController
should be
Class Blog::PostsController < ApplicationController
Upvotes: 1