Search code examples
ruby-on-railsfeed

Render show view from another controller


I have previewed all questions with similar topics and none of those solutions help me. I am attempting to create a twitter like feed that will display posts of a certain category in rails. This is my industries controller:

class IndustriesController < ApplicationController

  def index
    @wads = Wad.order('created_at DESC')
  end

  def music
    @music_wads = Wad.where(category: "Music").paginate(page: params[:page], per_page: 20)
    @wad = @music_wads.pluck(:id)
  end
end 

This is part of my posts controller:

class WadsController < ApplicationController
    before_action :find_wad, only: [:show, :edit, :update, :destroy]

    def index
        @wads = Wad.all
    end

    def show
        @wad = Wad.find(params[:id])
    end

    def new
        @wad = Wad.new
    end


    def create
        @wad = current_user.wads.build(wad_params)
        if @wad.save    
          redirect_to @wad
        else
          flash[:error] = 'Error try again'
          render 'new'
        end
    end
end 

And this is my show view for my industries controller:

<h1>Music Wads</h1>

<%= will_paginate @music_wads %>

<% @music_wads.each do |music_wad| %>
    <%= link_to 'wads/:id' do %>
    <div class="flex-rectangle">
        <%= music_wad.user.name  %>
        <%= music_wad.short_form %>
    <% end %>
    </div>
<%= will_paginate @music_wads %>
<% end %>

Here is my routes file:

Rails.application.routes.draw do

  root   'static_pages#home'
  get    '/help',    to: 'static_pages#help'
  get    '/about',   to: 'static_pages#about'
  get    '/contact', to: 'static_pages#contact'
  get    '/signup',  to: 'users#new'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  get    '/industries', to: 'industries#index'
  get    '/music',    to:  'industries#music'
  get    '/tech',     to:  'industries#tech'


  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
  resources :wads 
end

I am attempting to make it so that clicking on a post among the lists of post carries you to page of that post (/wads/id). I've been baffled all day and am now at my wits end. I am aware I am a noob and this is a nooby question but any help would gladly be appreciated. Thanks


Solution

  • resources :wads will create some routes that you can use.

    Running rails routes (on rails 5) or rake routes (on rails 4 and lower) in the console will give you the list of your routes.

    Under the prefix column you could find the correct route name you should use and under the URI Pattern you could see the actual address it links to.

    You asked for wads/:id so the link should be <%= link_to wad_path(wad_music) do %> (the prefix is wad_path and you need to give it the object which holds the id - or an id...)

    Since you want to link to a singular action - meaning that the action will get and id of an object - and get it (gets a single object!) the link prefix will be in singular form as well: wad_path and not wads_path

    (wads_path will link to the index action in the controller and doesn't need to get any object or id)