Getting nested resources from the Rails controller

0

Good evening, this is my first post here, I'm learning to use the tool, sorry if I did something wrong. But here's the problem ... I'm trying to make a "Trello" clone in Rails for a college discipline. Basically I have one in the Project models that a user creates, within that project, the user can create frames, called Boards , and finally, inside the boards, he you can create Stories / issues . At the moment, my route file looks like this.

Rails.application.routes.draw do
# Root Route
 root to: 'home#index'

# Devise Routes
 devise_for :users

# User Routes
 authenticate :user do
   namespace :users do
     root to: 'dashboard#index'

  resources :projects do
    resources :boards do
      resources :stories
    end
  end

  end
 end
end

What I want is that the action show of a project in projects_controller, show your pictures and within the frames your stories, something like this ...

IevenmanagedtodothisusingVueJS,butIwantthisparttobedoneonlywithRails.Toachievethis,IknowIneedtogetthestoriesfromaframeworkinprojects_controller,andthat'swhereI'mgettinglost.

Followmyprojects_controller.rb

classUsers::ProjectsController<Users::BaseControllerbefore_action:set_project,only:[:show,:edit,:update,:destroy]defindex@projects=current_user.projectsenddefeditenddefshow@[email protected]#@stories=@boards.???>thisguyherethatiscausingtheproblemenddefnew@project=current_user.projects.newenddefcreate@project=current_user.projects.new(projects_params)[email protected][:notice]="Projeto criado com sucesso"
      redirect_to [:users, @project]
    else
      flash[:error] = "Falha na criaçao do projeto"
      render :new
    end
  end

  def update
    if @project.update(projects_params)
      flash[:notice] = "Projeto atualizado com sucesso"
      redirect_to [:users, @project]
    else
      flash[:error] = "Falha na atualizaçao do projeto"
      render :edit
    end
  end

  def destroy
    @project.destroy
    flash[:notice] = "Projeto excluido com sucesso"
    redirect_to [:users, :projects]
  end

  private
  def set_project
    @project = current_user.projects.find(params[:id])
  end

  def projects_params
    params.require(:project).permit(:name)
  end
end

In both boards_controller and stories_controller , when the action create is called, redirect sends the projects_controller show , and I need it action, show the pictures and the stories. The pictures I can show the way it is done, my problem is with the same stories, that I can not get them and send them to view. The relationships are ok, because through the rails console I can create and access quietly.

Could someone give me a hand? I already lost 2 days with this problem.

    
asked by anonymous 04.05.2018 / 01:49

1 answer

0

From what I understand you want to use accepts_nested_attributes_for: author

link

within your Project model

has_many boards accepts_nested_attributes_for: boards

    
08.05.2018 / 22:49