Yul*_*uli 3 ruby-on-rails ruby-on-rails-3
多谢你们.我已经解决了这个问题.这是因为当我进入posts/new页面时.新操作会创建一个@post具有nil属性的虚拟对象.由于@post存在,会出现侧栏中的编辑和删除链接.但是,edit_post_path因为@post.id是不起作用nil.然后发生错误.所以我改变<% if @post %>了<% if @post && !@post.id.nil? %>它,它的工作原理. - ps rails错误消息非常混乱.
我是rails的新手,只是构建了一个简单的应用程序,当我点击链接创建新帖子时看到了错误:
No route matches {:action=>"edit", :controller=>"posts"}
该rake routes结果:
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
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
Run Code Online (Sandbox Code Playgroud)
routes.rb文件包含resources :posts在其中.
链接是: <li><%= link_to "New Post", new_post_path %></li>
新的编辑方法PostsController:
def new
@post = Post.new
end
def edit
@post = Post.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
我可以显示帖子,编辑帖子并删除帖子.但每当我想单击链接以创建新帖子时,就会发生错误.我无法弄清楚为什么new_post_path会导致'编辑'路径????
有人可以帮我吗?如果您需要更多代码,请告诉我.
谢谢!
更新
添加_sidebar.html.erb(抱歉格式,IDK的如何保持他们作为原来,有一些正常的nav,ul,li外标签)
new.html.erb
<%= form_for @post do |f| %>
<div class="field">
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.text_area :content, placeholder: "new post here..." %>
</div>
<div class="field">
<%= f.select :public, [['Public', true], ['Private', false]] %>
</div>
<%= f.submit "Post", class: "btn" %>
Run Code Online (Sandbox Code Playgroud)
<% end %>
我试过app.new_post_path,它表明/posts/new,我猜它很好.
您可能正在使用edit_post_path您new.html.erb的帖子.它抱怨no route matches你没有通过编辑帖子的ID.但这new首先不应该在视野中,所以你可能需要删除该行 - 用于编辑帖子