编辑:感谢您的答复,我尝试了下面列出的所有方法,但现在还是很幸运。如果输入localhosts/posts/new,它将带我到表单,但是当我从导航栏中单击时,该链接不起作用。我已经更新了代码,并包含了我的 rake 路结果。

我是 ruby 的新手,正在研究一个教程,但是我的链接之一无法正常工作,也不知道发生了什么。

我创建新帖子的导航链接无法将我带到正确的页面,当我单击指向“posts_path”的链接时,页面不会更改。

我可以通过在地址栏中输入/posts/new来创建新帖子,但是当我单击导航栏上的“新帖子”链接时,页面不会更新(尽管url显示/posts)。任何想法如何解决这个问题?

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

app/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 路:
password_resets_new     GET     /password_resets/new(.:format)          password_resets#new
password_resets_edit    GET     /password_resets/edit(.:format)         password_resets#edit
sessions_new            GET     /sessions/new(.:format)                 sessions#new
root                    GET     /                                       static_pages#home
search                  GET     /search(.:format)                       static_pages#search
login                   GET     /login(.:format)                        sessions#new
new_post                GET     /posts(.:format)                        posts#new
                        POST    /login(.:format)                        sessions#create
logout                  DELETE  /logout(.:format)                       sessions#destroy
signup                  GET     /signup(.:format)                       users#new
users_new               GET     /users/new(.:format)                    users#new
static_pages_home       GET     /static_pages/home(.:format)            static_pages#home
static_pages_about      GET     /static_pages/about(.:format)           static_pages#about
static_pages_search     GET     /static_pages/search(.:format)          static_pages#search
posts_new               GET     /posts/new(.:format)                    posts#new
                        GET     /password_resets/new(.:format)          password_resets#new
                        GET     /password_resets/edit(.:format)         password_resets#edit
                        GET     /sessions/new(.:format)                 sessions#new
help                    GET     /help(.:format)                         static_pages#help
about                   GET     /about(.:format)                        static_pages#about
contact                 GET     /contact(.:format)                      static_pages#contact
users                   GET     /users(.:format)                        users#index
                        POST    /users(.:format)                        users#create
new_user                GET     /users/new(.:format)                    users#new
edit_user               GET     /users/:id/edit(.:format)               users#edit
user                    GET     /users/:id(.:format)                    users#show
                        PATCH   /users/:id(.:format)                    users#update
                        PUT     /users/:id(.:format)                    users#update
                        DELETE  /users/:id(.:format)                    users#destroy
edit_account_activation GET     /account_activations/:id/edit(.:format) account_activations#edit
password_resets         POST    /password_resets(.:format)              password_resets#create
new_password_reset      GET     /password_resets/new(.:format)          password_resets#new
edit_password_reset     GET     /password_resets/:id/edit(.:format)     password_resets#edit
password_reset          PATCH   /password_resets/:id(.:format)          password_resets#update
                        PUT     /password_resets/:id(.:format)          password_resets#update
posts                   POST    /posts(.:format)                        posts#create
                        GET     /posts/new(.:format)                    posts#new
post                    DELETE  /posts/:id(.:format)                    posts#destroy

最佳答案

Rails从顶部到底部扫描路由,即route.rb 。只要找到匹配的路由,就会执行相应的 Controller #action ,其余所有路由都将被跳过(找到匹配项后的路由)。

在这种情况下,路线会发生冲突,因此波纹管变更也可以正常工作。
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' #not required as resources :posts will do the job in last line
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', to: 'posts#new', as: 'posts_new_path'  #Made changes here according to helper used in view 'posts_new_path'
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

关于ruby-on-rails - 路线无法正常运作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41456166/

10-15 17:48