加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails没有路由匹配

发布时间:2020-12-17 01:25:51 所属栏目:百科 来源:网络整理
导读:多谢你们.我已经解决了这个问题.这是因为当我进入帖子/新页面时.新操作会创建一个带有nil属性的虚拟@post.由于@post存在,编辑将出现侧栏中的删除链接.但是,edit_post_path不起作用,因为@ post.id为nil.然后发生错误.所以我刚刚更改了%if @post%到%if @pos
多谢你们.我已经解决了这个问题.这是因为当我进入帖子/新页面时.新操作会创建一个带有nil属性的虚拟@post.由于@post存在,编辑&将出现侧栏中的删除链接.但是,edit_post_path不起作用,因为@ post.id为nil.然后发生错误.所以我刚刚更改了<%if @post%>到<%if @post&& !@post.id.nil? %GT;它的工作原理. - p.s. rails错误消息非常混乱. 我是rails的新手,只是构建了一个简单的应用程序,当我点击链接创建新帖子时看到了错误:
没有路线匹配{:action =>“edit”,:controller =>“posts”}

耙路线结果如下:

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

routes.rb文件中包含资源:帖子.

链接是:< li><%= link_to“New Post”,new_post_path%>< / li>

新的& PostsController中的编辑方法:

def new
    @post = Post.new
  end
  def edit
    @post = Post.find(params[:id])
  end

我可以显示帖子,编辑帖子并删除帖子.但每当我想单击链接以创建新帖子时,就会发生错误.我无法弄清楚为什么new_post_path将导致’编辑’路径????

有人可以帮我吗?如果您需要更多代码,请告诉我.

谢谢!

更新

>添加_sidebar.html.erb(对不起格式,idk如何将它们保留为原始格式,外面有一些普通的nav,ul,li标签)

????
????????><%= link_to“Home”,root_path%>
????????><%= link_to“关于我”,about_path%>
????????<%if signed_in? %GT;
????????????
????????????????><%= link_to“New Post”,new_post_path%>
????????????????<%if @post%>
????????????????????><%= link_to“编辑帖子”,edit_post_path%>
????????????????????><%= link_to“删除帖子”,post_path(@ post),方法:“删除”,确认:“你确定要删除吗?” %GT;
????????????????<%end%>
????????????????><%= link_to“退出”,chulai_path,方法:“删除”%>
????????????
????????<%end%>
????
????

> new.html.erb

<%= form_for @post do | f | %GT;
???< 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" %>

<%end%>
>我试过app.new_post_path,它显示/ posts / new,我想这很好.

解决方法

你可能在new.html.erb中使用edit_post_path来发帖. 由于您没有通过编辑帖子的ID,因此抱怨没有路由匹配.但这首先不应该是新的视图,所以你可能需要删除该行 – 用于编辑帖子

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读