2016-09-14 66 views
0

我正在構建一個博客並實現了一個編輯帖子函數,但得到以下錯誤消息:ActiveRecord :: RecordNotFound at/posts/post/edit 找不到Post with'id'= post我正在使用rails 5我得到以下錯誤ActiveRecord :: RecordNotFound at/posts/post/edit找不到Post with'id'= post

在此先感謝您。

**Index.html.erb** 
<div class="banner-container"> 
<div class="container"> 
    <div class="banner-gradient-shadow"></div> 
     <div class="banner-content banner-font"> 
     <h1> 
      Some text 
     </h1> 
     <p> 
      <strong>More text</strong>. 
     </p> 
     </div> 
    </div> 
    </div> 
</div> 

<div class="container"> 
    <div class="row"> 
    <% @posts.each do |p| %> 
    <%= render "card", post: p %> 
    <% end %> 
    </div> 
</div> 

卡部分

<div class="container"> 
<div class="row"> 
    <div class="col-xs-12"> 
    <div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)), url('post.photo_url');"> 
    <div class="card-category"><%= post.created_at.strftime("Posted %a %m %b %Y at %I:%M%p") %></div> 
     <div class="card-description"> 
     <h2><%= post.title %></h2> 
     <p><%= post.summary %></p> 
     </div> 
     <!-- <img class="card-user avatar" src="user.jpg"> --> 
     <%= link_to "", post_path(post), class: "card-link"%> 
    </div> 
    <% if current_user %> 
    <%= link_to "Edit", edit_post_path(:post), :class => "btn btn- default" %> 
    <% end %> 
    </div> 
</div> 

edit.html.erb

<div class="container"> 
<div class="row"> 
    <div class="col-md-6 col-md-offset-3"> 
    <h1>Editing post</h1> 
    <%= simple_form_for :post do |f| %> 
    <%= f.input :title %> 
    <%= f.input :summary %> 
    <%= f.input :post_content, as: :text %> 
    <%= f.submit class: "btn btn-danger"%> 
    <% end %> 
    </div> 
</div> 

posts_controller.rb

class PostsController < ApplicationController 
before_action :set_post, only: [:show, :edit, :update] 
skip_before_action :authenticate_user!, only: [ :edit] 

def index 
@posts = Post.order("created_at DESC").all 
end 

def show 
@comment = Comment.new 
end 

def new 
@post = Post.new 
end 

def edit 

end 

def create 
@post = Post.new(post_params) 
if @post.save 
redirect_to post_path(@post) 
else 
render :show 
end 
end 

def update 
    if @post.update(post_params) 
    redirect_to @post 
    else 
    render :show 
    end 
end 


def destroy 
end 

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

def post_params 
params.require(:post).permit(:title, :post_content, :summary, photo: [], video: []) 
end 
end 

的routes.rb

Rails.application.routes.draw do 
devise_for :users 
mount Attachinary::Engine => "/attachinary" 
root to: 'posts#index' 
resources :posts, only: [:index, :show, :new, :create, :edit, :update] do 
resources :comments, only: [:show, :edit, :update, :create, :destroy] 
    end 
end 
+0

你嘗試'<(%)=的link_to 「編輯」,edit_post_path(post.id):類=>「BTN BTN - 默認「%>'? –

回答

0

變化:postpost卡部分

<%= link_to "Edit", edit_post_path(post), :class => "btn btn-default" %> 

Rails正在發送:post:id像{「身份證」 =>「:後」}值

而且set_params試圖與params[:id]發現它是「:後」

@post = Post.find(:post) 
+0

我確實嘗試過。由於某種原因,它在控制器上出現錯誤,當我刪除set_post動作進行編輯時,出現錯誤並讓我進入編輯窗體,但是當我向窗體添加數據時,我再次收到sam錯誤。希望有道理 –

+0

你試過改變它在你的卡片部分? –

+0

是的,我做到了這一點,無濟於事。當我渲染部分時,是否存在index.html.erb問題? –

0

我已經解決了這個問題與以下變化:

index.html.erb 沒有變化

卡部分

<div class="container"> 
<div class="row"> 
    <div class="col-xs-12"> 
    <div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)), url('post.photo_url');"> 
    <div class="card-category"><%= post.created_at.strftime("Posted %a %m %b %Y at %I:%M%p") %></div> 
     <div class="card-description"> 
     <h2><%= post.title %></h2> 
     <p><%= post.summary %></p> 
     </div> 
     <%= link_to "", post_path(post), class: "card-link"%> 
    </div> 
    <% if current_user %> 
    <%= link_to "Edit", edit_post_path(post), :class => "btn btn-default" %> 
    <% end %> 
    </div> 
    </div> 
</div> 

edit.html.erb

<div class="container"> 
<div class="row"> 
    <div class="col-md-6 col-md-offset-3"> 
    <h1>Editing post</h1> 
     <%= simple_form_for @post do |f| %> 
     <%= f.input :title, placeholder: "Enter a title" %> 
     <%= f.input :summary, placeholder: "Short summary of post" %> 
     <%= f.input :post_content, as: :text %> 
     <%= f.submit class: "btn btn-danger"%> 
     <% end %> 
    </div> 
</div> 
</div> 
相關問題