railstutorial(rails 4)编辑帖子给出路由错误“/posts.5”

时间:2013-11-18 10:13:01

标签: ruby-on-rails ruby ruby-on-rails-4

我正在使用本教程学习rails 4: http://guides.rubyonrails.org/getting_started.html

直到现在我可以解决所有问题,但现在我编辑帖子后出现问题,然后我想保存它,但它会将我发送到localhost:3000/posts.7(7是帖子ID)

当我浏览/发布/新建,然后点击提交按钮时,它可以正常工作

这是我得到的错误。

    Routing Error
    No route matches [PATCH] "/posts.7"

    Rails.root: /home/spod/railstutorial/blog
    Application Trace | Framework Trace | Full Trace

    Routes

    Routes match in priority from top to bottom
    Helper  HTTP Verb   Path    Controller#Action
    Path / Url 

    posts_path  GET     /posts(.:format)    posts#index
                POST    /posts(.:format)    posts#create
    new_post_path   GET     /posts/new(.:format)    posts#new
    edit_post_path  GET     /posts/:id/edit(.:format)   posts#edit
    post_path   GET     /posts/:id(.:format)    posts#show
                PATCH   /posts/:id(.:format)    posts#update
                PUT     /posts/:id(.:format)    posts#update
                DELETE  /posts/:id(.:format)    posts#destroy
    root_path   GET     /   welcome#index 

这里是posts_controller.rb

class PostsController < ApplicationController

 def terra
 end

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

 def update
  @post = Post.find(params[:id])

  if @post.update(params[:post].permit(:title, :text))
   rederict_to @post
  else
   render 'edit'
  end
 end

 def index
  @posts = Post.all
 end

 def create
   @post = Post.new(params[:post].permit(:title, :text))

   if @post.save
    redirect_to @post
   else
    render 'new'

   end
  end

  private
   def post_params
   params.require(:post).permit(:title, :text)
  end

 end

以下是edit.html.erb

<h1>Editing post</h1>

<%= form_for :post, url: posts_path(@post), method: :patch do |f| %>
 <% if @post.errors.any? %>
 <div id="error_explanation">
  <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
  <ul>
   <% @post.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
   <% end %>
  </ul>
 </div>
 <% end %>

 <p>
  <%= f.label :title %><br>
  <%= f.text_field :title %>
 </p>

 <p>
  <%= f.label :text %><br>
  <%= f.text_area :text %>
 </p>

 <p>
  <%= f.submit %>
 </p>
<% end %>

<% link_to 'Home', posts_path %>

这是new.html.erb

 <h1>New Post</h1>

 <%= form_for :post, url: posts_path do |f| %>
  <% if @post.errors.any? %>
  <div id="error_explanation">
   <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being      saved:</h2>
   <ul>
    <% @post.errors.full_messages.each do |msg| %>
     <li><%= msg %></li>
    <% end %>
   </ul>
  </div>
  <% end %>

  <p>
   <%= f.label :title %><br>
   <%= f.text_field :title %>
  </p>

  <p>
   <%= f.label :text %><br>
   <%= f.text_field :text %>
  </p>

  <p>
  </p>
 <% end %>


 <%=link_to 'Home', posts_path %>

我希望这就是你需要的一切 谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

edit.html.erb

<%= form_for :post, url: edit_post_path(@post), method: :patch do |f| %>
 <% if @post.errors.any? %>
 <div id="error_explanation">
  <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
  <ul>
   <% @post.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
   <% end %>
  </ul>
 </div>
 <% end %>

您需要指定指向正确操作的正确路径

答案 1 :(得分:0)

嗯,试试这个

<%= form_for @post, url: {action: "update"} do |f| %>

而不是

<%= form_for :post, url: posts_path(@post), method: :patch do |f| %>

希望它能在你的最终实现。