EDIT: Thanks for the answers, I tried all of the following, but good luck now. If I type localhosts / posts / new, it takes me to the form, however the link does not work when I click on the navigation bar. I updated the code and included the results of my rake routes.
I am new to ruby and working on a tutorial, however one of my links is broken and not sure what is going on.
My navigation link for creating a new post does not lead me to the correct page, when I click on the "posts_path" link, the page does not change.
I can create new posts by typing / posts / new in the address bar, but when I click the "New post" link in the navigation bar, the page does not refresh (although the url displays / posts posts). Any idea how to fix this?
config / routes.rb
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/search', to: 'static_pages#search'
get '/login', to: 'sessions#new'
get '/posts', to: 'posts#new', as: 'new_post'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
get 'users/new'
get 'static_pages/home'
get 'posts/new'
get 'sessions/new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:new, :create, :destroy]
end
application / views / layouts / _header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<% link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Search", search_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", users_path %></li>
<li><%= link_to "Posts", posts_new_path %></li>
<li class="dropdown">
<a href='#' class="dropdown-toggle" data-toggle="dropdown">Account <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li><%= link_to "Logout", logout_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
posts_controller.rb
class PostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@post.destroy
flash[:success] = "Post Deleted"
redirect_to request.referrer || root_url
end
def new
@post = current_user.posts.build if logged_in?
end
private
def post_params
params.require(:post).permit(:description, :picture)
end
def correct_user
@post = current_user.posts.find_by(id:params[:id])
redirect_to root_url if @post.nil?
end
end
rake routes:
password_resets_new GET /password_resets/new(.:format) password_resets
password_resets_edit GET /password_resets/edit(.:format) password_resets
sessions_new GET /sessions/new(.:format) sessions
root GET / static_pages
search GET /search(.:format) static_pages
login GET /login(.:format) sessions
new_post GET /posts(.:format) posts
POST /login(.:format) sessions
logout DELETE /logout(.:format) sessions
signup GET /signup(.:format) users
users_new GET /users/new(.:format) users
static_pages_home GET /static_pages/home(.:format) static_pages
static_pages_about GET /static_pages/about(.:format) static_pages
static_pages_search GET /static_pages/search(.:format) static_pages
posts_new GET /posts/new(.:format) posts
GET /password_resets/new(.:format) password_resets
GET /password_resets/edit(.:format) password_resets
GET /sessions/new(.:format) sessions
help GET /help(.:format) static_pages
about GET /about(.:format) static_pages
contact GET /contact(.:format) static_pages
users GET /users(.:format) users
POST /users(.:format) users
new_user GET /users/new(.:format) users
edit_user GET /users/:id/edit(.:format) users
user GET /users/:id(.:format) users
PATCH /users/:id(.:format) users
PUT /users/:id(.:format) users
DELETE /users/:id(.:format) users
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations
password_resets POST /password_resets(.:format) password_resets
new_password_reset GET /password_resets/new(.:format) password_resets
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets
password_reset PATCH /password_resets/:id(.:format) password_resets
PUT /password_resets/:id(.:format) password_resets
posts POST /posts(.:format) posts
GET /posts/new(.:format) posts
post DELETE /posts/:id(.:format) posts