2013-03-22 44 views
0

我一直在做一個rails教程(在lynda上),我一直在研究一個簡單的cms。它有一個控制器和模型的主題,我可以保存使用導軌控制檯沒有問題的主題。但是,當我嘗試使用Web表單創建新主題時,出現此錯誤:The action '#<ActiveRecord::Relation:0x2166e28>' could not be found for SubjectsController。 主題模型有一個單一的關係(has_many: pages),但不應該影響這個,因爲沒有外鍵的網絡表格正在保存。保存Web表單時未找到控制器的Rails操作

爲新創建的外觀像這樣的控制器方法:

def new 
    @subject = Subject.new 
end 

def create 
    #Instantiate a new object using form params 
    @subject = Subject.new(params[:subject]) 

    #Save the object 
    if @subject.save 

     #If save succeds, redirect to list action 
     redirect_to(action: list) 
    else 

     #If save fails, redisplay the form so user can fix problems 
     render('new') 
    end 
end 

一些很重要:控制器成功保存了新的課題。它只是產生而不是重定向

的查看web表單此錯誤是這樣的:

<html> 
    <%= link_to("<< Back to List", {action:'list'}, class: 'back-link')%> 

    <div class="subject new"> 
     <h2>Create Subject</h2> 

     <%= form_for(:subject, url: {action: 'create'}) do |f|%> 

     <table summary="Subject form fields"> 
      <tr> 
       <th>Name</th> 
       <td><%= f.text_field(:name) %></td> 
      </tr> 
      <tr> 
       <th>Position</th> 
       <td><%= f.text_field(:position) %></td> 
      </tr> 
      <tr> 
       <th>Visible</th> 
       <td><%= f.text_field(:visible) %></td> 
      </tr> 
     </table> 

     <div class="form-buttons"> 
      <%= submit_tag("Create Subject") %> 
     </div> 
     <% end %> 
    </div> 
</html> 
+0

請張貼你的觀點的代碼有它的形式 – 2013-03-22 21:16:10

+0

那裏。我已經添加了它。 – Amja 2013-03-22 21:20:16

回答

1

你應該重定向到:list而不是list,或更好

redirect_to(action: 'list')