Getting nested Sources in the Rails controller

Asked

Viewed 78 times

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 clone of "Trello" in Rails for a college course. Basically I have one in the models a Project that a user creates, within this project, the user can create frames, called Boards, And finally, within the Boards, it can create Stories/issues. At the moment, my route file is as follows.

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 the action show of a project in projects_controller, show your frames and within the frames your stories, something like this... inserir a descrição da imagem aqui

I even managed to do this using Vuejs, but I want this part to be done only with Rails. To achieve this, I know I need to get the stories of a painting in the projects_controller, and that’s where I’m losing myself.

Follow my projects_controller.Rb

class Users::ProjectsController < Users::BaseController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  def index
    @projects = current_user.projects
  end

  def edit
  end

  def show
    @boards = @project.boards.sorted
    #@stories = @boards.??? >this guy here that is causing the problem
  end

  def new
    @project = current_user.projects.new
  end

  def create
    @project = current_user.projects.new(projects_params)

    if @project.save
      flash[: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

Both in the boards_controller how much in the stories_controller, when the create action is called, redirect sends the projects_controller, and I need in this action, to show the paintings and the stories. The pictures I can show the way it’s being done, my problem is with the stories themselves, that I’m not able to pick them up and send them to view. Relationships are ok, because through the Rails console I can create and access quietly.

Can someone give me a hand? I’ve already lost 2 days with this problem.

  • Sorry, I’m new here, I thought it was kind of global. Here’s the problem. As you can see on my routes, I have 3 nested resources. And I need to show the Boards and Stories in my projects_controller’s action show. In theory, I need to take all the stories of a particular board that is within a particular project. My problem is that I’m not getting the board stories and play for view.

  • @Felipekosouski you can edit your question, also ask the Tour to learn how the site works.

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.