2014-07-14 67 views
1

我無法讓重定向和錯誤消息生效。從我讀過的你不能得到一個表單錯誤,當你使用重定向顯示,所以我試圖在失敗後使用渲染。顯示錶單錯誤消息

我在主題頁上有一個新帖子表單。網址是「topic/1」。如果您對該主題發表了一篇文章,並且輸入內容出了問題,我希望它回到topic/1的頁面,並顯示錯誤,我無法弄清楚如何讓它恢復。重定向(:後退)做我想要的,但不顯示錶單錯誤。

話題的show.html網頁上的表格:

<%= form_for(@post) do |f| %> 
<%= render 'shared/post_error_messages' %> 

<%= f.label :title, "Post Title" %> 
<%= f.text_field :title %> 

<%= f.label :content %> 
<%= f.text_field :content %> 

<%= f.hidden_field :parent_id, value: 0 %> 
<%= f.hidden_field :topic_id, value: @topic.id %> 
<%= f.hidden_field :user_id, value: current_user.id %> 

<%= f.submit "Create Post" , class: "btn btn-small btn-primary" %> 
<% end %> 

創建的帖子控制器

def create 

    @post = Post.new(post_params) 
    @topic = Topic.find_by(id: params[:topic_id]) 
    if @post.save 
    redirect_to @post 
else 

    @topic = Topic.new 
    render "/topics/show" 
end 
end 

我想我主要是試圖做從該ID的渲染作用該表格最初的頁面。

+0

您必須告訴方法要呈現的內容。在這種情況下,我猜這是':new'模板。 – Omni

+0

我遺漏了我所擁有的東西,因爲它不起作用,我正在嘗試一些不同的想法,我用我現在的東西編輯了原始文章。它顯示錯誤消息和表單,但我不能讓它顯示頁面的其餘部分,即與該主題ID相關聯的帖子列表。 – Matthew

+0

'@topic = Topic.new'在創建時不需要使用'render'new'' – Nithin

回答

1

錯誤

問題是沒有什麼做你的方式渲染表格(renderredirect) - 這與您處理對象的方式有關。

當您使用form_for時,Rails會將任何錯誤追加到@active_record_object.errors方法中。這將允許您調用下面:

form_for error messages in Ruby on Rails

<%= form_for @object do |f| %> 
    <% @location.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
<% end %> 

這隻能如果你正確地創建ActiveRecord對象,你似乎做

-

嵌套

#config/routes.rb 
resources :topics do 
    resources :posts, path: "", path_names: {new: ""}, except: [:index] #-> domain.com/topics/1 
end 

你會使用following setupnested路線好得多:

<%= form_for [@topic, @post] do |f| %> 
    ... 
<% end %> 

這可以讓你創建一種形式,將路由到topics_posts_path,這基本上是你所需要的。然後,控制器將通過以下方式來平衡:

#app/controllers/topics_controller.rb 
Class TopicsController < ApplicationController 
    def new 
     @topic = Topic.find params[:topic_id] 
     @post = Post.new 
    end 
    def create 
     @topic = Topic.find params[:topic_id] 
     @post = Post.new post_params 
    end 

    private 

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

這讓我走上了正確的道路。我拿出路徑:path_names expect部分。它給了我一個關於不在那裏的觀點的錯誤。我認爲它是因爲我正在使用展示視圖獲取主題,並在帖子中使用新創建的新帖子。但是,一切似乎都以我想要的方式工作。重定向和網址都是正確的。 – Matthew

0

您正在使用全新的空白覆蓋原來發現的主題 - 這不應該是必需的,而且這會導致與其相關的帖子消失。

此外 - 如果您的主題和帖子是相關的 - 您應該在適當的協會@topic.posts而不是主要的Post類創建帖子。

@topic.posts.new意味着帖子的topic-id會自動更新爲@ topic.id ...的值,這意味着您無需將其設置在表單的隱藏字段中。實際上,如果你不這樣做,它會更好 - 只是完全刪除隱藏的字段。

如果你在第一次添加這個功能時(例如在主題/展示中),那麼你不需要將值傳遞給隱藏字段。

另外我也會爲服務器端的所有其他隱藏字段做同樣的事情。你真的不希望用戶使用Firebug破解的形式,並添加一些其他用戶的ID ......因此,無論是在創建行動,不與隱藏字段打擾

這應該工作:

def create  
    @topic = Topic.find_by(id: params[:topic_id]) 

    @post = @topic.posts.new(post_params) 
    @post.user = current_user 
    @post.parent_id = 0 

    if @post.save 
     redirect_to @post 
    else 
     render "/topics/show" 
    end 
end 

,如果它不 - 讓我知道了什麼錯誤信息,你得到(這樣我們可以調試)

+1

其他嵌套路徑的帖子對此有幫助。感謝關於隱藏字段的提示,並與關係一起創建。我沒有想到這一點。 – Matthew