"posts"} 即使它不在我的 paths.rb 文件中。帮助?" />

我发现没有路由匹配 {:action=>"new", :controller=>"posts"} 即使它不在我的 paths.rb 文件中。帮助?

发布于 2024-12-02 02:13:02 字数 6120 浏览 1 评论 0 原文

好的,这是我发布到 stackoverflow 的第一个问题。我对编码还很陌生,过去 3 个月一直在学习 Ruby on Rails。我的代码可能有一些问题,但就这样了。

基本上我正在尝试让用户发帖。我正在使用 Devise 进行注册,所有这些都有效。但是,当我在标题视图中创建指向“创建帖子”的链接时,它告诉我没有匹配的路线 =>尽管我认为它存在。我相信我错过了一些小东西,但在我为尝试使其正确而所做的所有调试中,我认为我可能在此过程中搞砸了其他东西。下面附上了我的routes.rb 代码、我的post_controller 文件和我的布局视图文件。抱歉,我的胡言乱语,我无法简明扼要。有人看出有什么问题吗?如果您需要查看其他代码,请告诉我

_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 %>

paths.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 paths

  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"}

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"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-12-09 02:13:02

尝试在控制台中输入rake paths并检查您要查找的路由是否存在。请注意,顺序也很重要。

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

热鲨 2024-12-09 02:13:02

本来应该是

<%= link_to "Create Post", new_user_post_path(current_user) %>

It should have been

<%= link_to "Create Post", new_user_post_path(current_user) %>
冰雪梦之恋 2024-12-09 02:13:02

我认为这是因为争论!
在您的路由文件中,您似乎还传递了 :user_id 。
在您的链接中尝试传递该 ID。
希望它有帮助...

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

另外我建议尝试重命名路径并使用它。
我知道这不是正确的方式,但仍然如此。
我遇到过类似的问题并通过这样做解决了!;)

希望它有帮助!

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文