2

OK so this is my first question posted to stackoverflow. I'm pretty new to coding, been learning Ruby on Rails for the past 3 months. There could be a few things wrong with my code but here it goes.

Basically I'm trying to get a User to Post. I'm using Devise for registration, and all of that works. But when I create a link to "Create Post" in my Header View, it tells me I don't have a route matching => even though I think it exists. I'm missing something small I believe but in all the debugging I've done to try and get it right, I think I might have messed something else up along the way. Below is attached my code for the routes.rb, my post_controller file, and my layout view file. Sorry about all the rambling, I couldn't be very concise. Does anyone see anything wrong? Let me know if you need to see other code

_header.html.erb

    <% if user_signed_in? %>
    Signed in as <%= current_user.username %>. Not you?
    <%= link_to "Logout", destroy_user_session_path, :method => :delete, %>
    <%= link_to "Create Post", new_user_post_path %>
    <%= link_to "Search", posts_index_path %>
    <%= link_to "Show All", posts_show_path %>

routes.rb

  #devise_for :users 
  devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end

  match '/users/:user_id/posts/new',  :to => 'posts#new'

  resources :users do
   resources :posts, :only => [:new, :create, :show, :index, :destroy]
  end

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'
  match '/safety',  :to => 'pages#safety'
   match '/privacy', :to => 'pages#privacy'

  get 'posts/index'
  get 'posts/show'
  get 'posts/post'

  match 'posts/search',   :to => 'posts#search'

  root :to => 'pages#home'

posts_controller

  def create
if signed_in?
  @user = current_user.posts.build(params[:user][:post])
  if @user.save
    flash[:success] = "Thanks for creating your post! " +
    redirect_to new_user_post_path(@post)
  else
    render 'new'

  def new   
   @title = "Create Post"
   @post = Post.new
  end

rake routes

  users_sign_out GET    /users/sign_out(.:format)           {:controller=>"devise/sessions",         :action=>"destroy"}
    new_user_session GET    /users/sign_in(.:format)            {:action=>"new", :controller=>"devise/sessions"}
        user_session POST   /users/sign_in(.:format)            {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format)           {:action=>"destroy", :controller=>"devise/sessions"}
       user_password POST   /users/password(.:format)           {:action=>"create", :controller=>"devise/passwords"}
   new_user_password GET    /users/password/new(.:format)       {:action=>"new", :controller=>"devise/passwords"}
  edit_user_password GET    /users/password/edit(.:format)      {:action=>"edit", :controller=>"devise/passwords"}
                     PUT    /users/password(.:format)           {:action=>"update", :controller=>"devise/passwords"}
     cancel_user_registration GET    /users/cancel(.:format)             {:action=>"cancel",    :controller=>"devise/registrations"}
     user_registration POST   /users(.:format)                    {:action=>"create", :controller=>"devise/registrations"}
     new_user_registration GET    /users/sign_up(.:format)            {:action=>"new", :controller=>"devise/registrations"}
     edit_user_registration GET    /users/edit(.:format)               {:action=>"edit", :controller=>"devise/registrations"}
                     PUT    /users(.:format)                    {:action=>"update", :controller=>"devise/registrations"}
                     DELETE /users(.:format)                    {:action=>"destroy", :controller=>"devise/registrations"}
                            /users/:user_id/posts/new(.:format) {:controller=>"posts", :action=>"new"}
          user_posts GET    /users/:user_id/posts(.:format)     {:action=>"index", :controller=>"posts"}
                     POST   /users/:user_id/posts(.:format)     {:action=>"create", :controller=>"posts"}
       new_user_post GET    /users/:user_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
           user_post GET    /users/:user_id/posts/:id(.:format) {:action=>"show", :controller=>"posts"}
                     DELETE /users/:user_id/posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
               users GET    /users(.:format)                    {:action=>"index", :controller=>"users"}
                     POST   /users(.:format)                    {:action=>"create", :controller=>"users"}
            new_user GET    /users/new(.:format)                {:action=>"new", :controller=>"users"}
           edit_user GET    /users/:id/edit(.:format)           {:action=>"edit", :controller=>"users"}
                user GET    /users/:id(.:format)                {:action=>"show", :controller=>"users"}
                     PUT    /users/:id(.:format)                {:action=>"update", :controller=>"users"}
                     DELETE /users/:id(.:format)                {:action=>"destroy", :controller=>"users"}
             contact        /contact(.:format)                  {:controller=>"pages", :action=>"contact"}
               about        /about(.:format)                    {:controller=>"pages", :action=>"about"}
                help        /help(.:format)                     {:controller=>"pages", :action=>"help"}
              safety        /safety(.:format)                   {:controller=>"pages", :action=>"safety"}
             privacy        /privacy(.:format)                  {:controller=>"pages", :action=>"privacy"}
         posts_index GET    /posts/index(.:format)              {:controller=>"posts", :action=>"index"}
          posts_show GET    /posts/show(.:format)               {:controller=>"posts", :action=>"show"}
          posts_post GET    /posts/post(.:format)               {:controller=>"posts", :action=>"post"}
        posts_search        /posts/search(.:format)             {:controller=>"posts", :action=>"search"}
                root        /(.:format)                         {:controller=>"pages", :action=>"home"}
abhi
  • 139
  • 1
  • 7

3 Answers3

1

Try typing rake routes in the console and check if the route you are looking for exists. Note that the order also matters.

Benjamin Tan Wei Hao
  • 9,621
  • 3
  • 30
  • 56
1

It should have been

<%= link_to "Create Post", new_user_post_path(current_user) %>
dexter
  • 13,365
  • 5
  • 39
  • 56
  • ah yup thanks @dexter. but now it says i'm getting an exception on my "new" variable... 'code' def new '@'title = "Create Post" '@'post = Post.new end – abhi Aug 30 '11 at 02:03
0

I think its because of the argument ! In your routes file it seems you are passing :user_id also. In your link try passing that id. Hope it helps...

<%= link_to "create Post", new_user_post_path(current_user.id) %>

Also I would suggest try renaming the path and use it. I know its not proper way but still. I had faced a similar problem and solved by doing this !!;)

Hope it helps !

  • ah yeah, i'm now seeing the right url path, but i'm getting this message: Users/abhisharma/rails_projects/projectx/app/controllers/posts_controller.rb:21: syntax error, unexpected tINTEGER, expecting ';' or '\n' ...1111111111111111111111111111111qqqqqqqqqqqqqqqqqqqqqqqqqqqqq... why is that showing up? i haven't seen that before. thanks for the help again! – abhi Aug 30 '11 at 01:36
  • which is referring to my def new code, which i updated in my code. – abhi Aug 30 '11 at 02:03