2016-09-26 95 views
0

我可以在我的控制檯看到,我做的erorr_messages部分是呈現,如果一個評論沒有通過驗證,那麼它不會被張貼,但我不能得到實際要呈現的錯誤內容。Rails:錯誤消息部分不呈現

錯誤部分:

<% if object.errors.any? %> 
<div id="error_explanation"> 
    <div class="alert alert-danger"> 
     The form contains <%= pluralize(object.errors.count, "error") %> 
    </div> 
    <ul> 
    <% object.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
<% end %> 

評論形式

<%= form_for @comment, url: comments_path do |f| %> 
<%= render 'shared/error_messages', object: f.object %> 
<%= f.hidden_field :user_id, value: current_user.id %> 
<%= f.hidden_field :post_id, value: post.id %> 
<%= f.text_area :content, size: "60x2", placeholder: "Comment on this post..." %> 
<%= f.submit "Comment" %> 

POST表單

<%= form_for [@user, @post] do |f| %> 
<%= render 'shared/error_messages', object: f.object %> 
<%= f.text_area :content, size: "60x12", placeholder: "What do you want to say?" %> 
<%= f.submit "Post" %> 

用戶/節目

<% if @user == current_user %> 
<h4>Welcome <%= current_user.email %>! </h4> 
<%= render "notifications" %> 
<%= render 'shared/post_form' %> 
<%= render 'feed' %> 
<% end %> 

class CommentsController < ApplicationController 
def index 
    @comments = Comment.all 
end 

def new 
    @comment = Comment.new 
    @user = User.find(params[:user_id]) 
end 

def create 
    @user = current_user 
    @comment = @user.comments.build(comment_params) 

    if @comment.save 
     flash[:success] = "Comment Posted!" 
     redirect_back(fallback_location: root_path) 
    else 
     flash[:danger] = "Could not post comment" 
     redirect_back(fallback_location: root_path) 
    end 
end 


private 

def comment_params 
    params.require(:comment).permit(:content, :user_id, :post_id) 
end 
end 

class PostsController < ApplicationController 

def index 
    @posts = Post.all 
    @user = User.find(params[:user_id]) 
    @comment = Comment.new 
end 

def new 
    @post = Post.new 
    @user = User.find(params[:user_id]) 
end 

def create 
    @post = current_user.posts.build(post_params) 
    if @post.save 
     flash[:success] = "Posted!" 
     redirect_to user_path(current_user) 
    else 
     flash[:danger] = "Post could not be submitted" 
     redirect_to users_path 
    end 
end 

private 

def post_params 
    params.require(:post).permit(:content) 
end 
end 
+0

你是否檢查過你的偏好文件名,它應該是'_error_messages'並且位於'app/views/shared'? – araratan

+0

是的,我已經將它命名爲,並且它也位於該位置。 –

回答

1

在你CommentsController#create,當保存失敗,而不是重定向:

redirect_back(fallback_location: root_path) 

嘗試停留在同一個頁面上,只是呈現了「新」的模板:

render action: "new" 

如果您重定向,瀏覽器會進行第二次請求和@comment將被覆蓋用新建造的評論。

如果您保持在同一頁面上並呈現「新」模板,它將使用已加載且未能保存的實例(此實例上設置了所有驗證錯誤)@comment

P.S.因爲這就是flash的作用 - 這是一種在會話中存儲消息的方式,以便它們可以在重定向中存活。

+0

唯一的問題是,我沒有用於new.html.erb的模板,因爲我剛剛使用了_comments_form.html。 erb部分 –

+0

啊,呃!在這種情況下,最簡單的選擇是將驗證錯誤消息放入flash中:'flash [:danger] =「無法發表評論:#{@comment.errors.full_messages}」。更復雜的選擇是使用[jquery_ujs](https://github.com/rails/jquery-ujs/wiki)和[rails'data:remote forms](http://guides.rubyonrails.org/working_with_javascript_in_rails。 html) – gmcnaughton

+0

搖滾男人的好主意把它放在閃存中!謝謝你的幫助! –