2
was researching but did not find any Generator to nested Resources in Rails. Would anyone know me if you know any?
2
was researching but did not find any Generator to nested Resources in Rails. Would anyone know me if you know any?
2
Henrique, there is no nested Resources generator, but Rails makes it easy for you to do this by hand.
In case Article has many Comments
, you would basically need to change:
a) The models:
class Article < ActiveRecord::Base
has_many :comments
end
class Article < ActiveRecord::Base
belongs_to :article
end
b) The routes:
resources :articles do
resources :comments
end
c) And finally, the controllers: (that part might be more)
comments_controller.rb
class CommentsController < ApplicationController
def index
@article = Article.find(params[:article_id])
@comments = @article.comments
end
def new
@article = Article.find(params[:article_id])
@comment = @article.comments.build
end
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
if @comment.save
flash[:notice] = "Successfully created comment."
redirect_to article_url(@comment.article_id)
else
render :action => 'new'
end
end
def edit
@comment = Comment.find(params[:id])
end
def update
@comment = Comment.find(params[:id])
if @comment.update_attributes(params[:comment])
flash[:notice] = "Successfully updated comment."
redirect_to article_url(@comment.article_id)
else
render :action => 'edit'
end
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
flash[:notice] = "Successfully destroyed comment."
redirect_to article_url(@comment.article_id)
end
end
articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.find(:all)
end
def show
@article = Article.find(params[:id])
@comment = Comment.new(:article => @article)
end
def new
@article = Article.new
end
def create
@article = Article.new(params[:article])
if @article.save
flash[:notice] = "Successfully created article."
redirect_to @article
else
render :action => 'new'
end
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash[:notice] = "Successfully updated article."
redirect_to @article
else
render :action => 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
flash[:notice] = "Successfully destroyed article."
redirect_to articles_url
end
end
Following the model illustrated above you should have no problems =)
Any more specific question, you can ask a new question!
Browser other questions tagged ruby-on-rails
You are not signed in. Login or sign up in order to post.
You probably won’t find it. Adding nested_attributes_for is one line. It’s easier to create both models and do it manually.
– hsgubert
I get it. I was actually looking for a Generator for the controller, who already knew how to generate one that carries the parent model and defines the functions in its scope.
– Henrique Cardoso
you say something like that:
SomeModule::Class
?– Bruno Casali
Not exactly. A Rails Generator...
– Henrique Cardoso