Brendon Baughn
Brendon Baughn

Reputation: 150

RoR link_to a different file

I made a file in my posts view called lists.html.erb, and it lists all the different posts. In my index.html.erb, it lists up to 5 posts. The button is supposed to link to multiple posts, but I get an error saying that I have an undefined local variable or method. I am trying:

<%= link_to "All Posts", lists_path %>

and

<%= link_to "All Posts", lists %>

For a question

Prefix Verb   URI Pattern                    Controller#Action
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   posts GET    /posts(.:format)               posts#index
                         POST   /posts(.:format)               posts#create
                new_post GET    /posts/new(.:format)           posts#new
               edit_post GET    /posts/:id/edit(.:format)      posts#edit
                    post GET    /posts/:id(.:format)           posts#show
                         PATCH  /posts/:id(.:format)           posts#update
                         PUT    /posts/:id(.:format)           posts#update
                         DELETE /posts/:id(.:format)           posts#destroy
                    root GET    /                              posts#index
                         GET    /*path(.:format)               redirect(301, /)

Posts_controller.rb

class PostsController < ApplicationController

before_action :find_post, only: [:show, :edit, :update, :destroy]
def index
    @posts = Post.all.order('created_at DESC')
end

def new
    @post = Post.new

    if @post.save
        redirect_to @post
    else
        render 'new'
    end
end

def create
    @post = Post.new(post_params)
    if @post.save
        redirect_to @post, notice: "Post was saved"
    else
        render 'new', notice: "I could not save the post. Call me for help if it keeps happening"
    end
end

def show
    #@post = Post.find(params[:id])
end

def edit
    #@post = Post.find(params[:id])
end

def lists
    @posts = Post.all.order('created_at DESC')
end

def update
    @post = Post.find(params[:id])

    if @post.update(params[:post].permit(:title, :body))
        redirect_to @post
    else
        render 'edit'
    end
end

def destroy
    #@post = Post.find(params[:id])
    if @post.destroy
        redirect_to root_path
    else
        redirect_to post_path, notice: "I couldn't be deleted for some reason. Try again or contact me"
    end
end


private
def post_params
    params.require(:post).permit(:title, :body, :image, :slug)
end

def find_post
    @post = Post.friendly.find(params[:id])
end
end

Upvotes: 0

Views: 27

Answers (1)

Vucko
Vucko

Reputation: 20834

You have the method in your controller, but you didn't put the route for it:

def lists
    @posts = Post.all.order('created_at DESC')
end

You can add a simple route, like

get 'lists' => 'posts#lists', :as => :lists

And if you run rake routes it will show this new route - lists_path so <%= link_to "All Posts", lists_path %> will work.

Upvotes: 1

Related Questions