2

estava pesquisando mas não encontrei nenhum generator para nested resources no Rails. Alguém saberia me dizer se conhece algum?

4
  • Você provavelmente não vai encontrar. Adicionar nested_attributes_for é uma linha só. Mais facil criar os dois models e fazer manualmente.
    – hsgubert
    22/03/2015 às 20:21
  • Entendo. Estava na verdade procurando um generator para o controlador, que já soubesse como gerar um que carrega o modelo pai e define as funções no escopo dele. 23/03/2015 às 21:27
  • tu diz algo assim: SomeModule::Class ? 9/04/2015 às 11:48
  • Não exatamente. Um generator do Rails... 14/04/2015 às 20:39

1 Resposta 1

2

Henrique, não existe gerador de nested resources, mas o Rails te facilita muito a fazer isso na mão.

Para o caso de Article has many Comments, você basicamente precisaria alterar:

a) Os models:

class Article < ActiveRecord::Base
  has_many :comments
end

class Article < ActiveRecord::Base
  belongs_to :article
end

b) As rotas:

resources :articles do
  resources :comments
end

c) E, finalmente, os controllers: (essa parte pode ser mais tricky)

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

Seguindo o modelo ilustrado acima você não deveria ter problemas =)

Qualquer dúvida mais específica, você pode fazer uma nova pergunta!

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .