2017-03-10 19 views
0

從我的理解,你可以使用渲染在視圖中有一個視圖。我一直試圖使用它的地方是有一個顯示頁面,並且在這個頁面上有一些按鈕。一個按鈕可以顯示/隱藏創建活動的表單。本質上,頁面將顯示活動並允許用戶通過單擊相應的按鈕並填寫表單來創建更多活動。Rails - 用不同的動作呈現視圖?

我試圖呈現對應於「新」動作「新」的頁面,但我已經遇到了以下問題

NoMethodError活動#顯示

未定義的方法`TO_KEY」對於 活動:: ActiveRecord_Relation:0x694e270

此錯誤搜索,我認爲它涉及到我的活動控制器中的「顯示」行動,以及它如何不指望一個「form_對於」。我不知道如何解決這個問題,或者如果我正在使用正確渲染。

我想知道的是,如果這是可能的或者推薦的,因爲我看到的例子似乎在html之間存在共享元素時使用渲染,所以使用渲染會減少代碼。顯示頁面將是唯一具有創建活動表單的頁面,所以我可以簡單地添加html代碼。

任何意見或幫助將不勝感激,謝謝!

活動顯示HTML:

<nav aria-label="..."> 

    <% @activity.each do |activity| %> 

     <div class = "row text-center"> 
     <div class = 'col-md-4'> 
      <h3> 
      <%= activity.a_name %> 
      </h3> 
     </div> 
     </div> 

    <% end %> 

    <div class="panel-group"> 
    <div class="panel-body"> 
     <h2 align = "center">Activities</h2> 
    </div> 
    <div class="panel-group"> 
     <div class="panel text-center"> 
     <div class="panel-heading"> 
      <h4 class="panel-title"> 
      <a data-toggle="collapse" href="#collapse1">Collapsible list group</a> 
      </h4> 
     </div> 
     <div id="collapse1" class="panel-collapse collapse"> 
      <ul class="list-group"> 
      <li class="list-group-item"> 
       <h1>ONE</h1> 
      </li> 
      <li class="list-group-item">Two</li> 
      <li class="list-group-item">Three</li> 
      </ul> 
      <div class="panel-footer">Footer</div> 
     </div> 
     </div> 

    </div> 
    </div> 


    <ul class="pager"> 

    <li class="previous"><a href="#"><span aria-hidden="true">&larr;</span>Previous </a></li> 
    <li class="next"><a href="#"> Next<span aria-hidden="true">&rarr;</span></a></li> 
    <div class="btn-group" role="group" aria-label="..."> 
     <a href="new" class="btn_act">Add Activity</a> 
    </div> 

    <% render 'activities/new'%> 

    </ul> 


</nav> 

活動_new HTML:

<body> 
<% flash.each do |key, value| %> 
    <div class="alert alert-<%= key %>"><%= value %></div> 
<% end %> 

<%= form_for @activity do |a| %> 
    <div class = "form-group"> 
     <div class = "row top-buffer text-center"> 
     <div class='col-md-3'> 
      <%= a.label :a_name, 'Activity Name' %>: 
      <%= a.text_field :a_name %> 
     </div> 
     </div> 

     <div class ="row top-buffer text-center"> 
     <div class="col-md-3"> 
      <%= a.submit 'Create', class: 'btn btn-primary'%> 
     </div> 
     </div> 
    </div> 
<% end %> 

</body> 

活動控制器:

class ActivitiesController < ApplicationController 
    def display 
    @activity = Activity.all 

    end 

    def index 
    @activity = Activity.all 
    end 

    def show 
    @activity = Activity.all 
    end 

    def new 
    @activity = Activity.new 
    end 

    def create 
    @activity = Activity.create(activity_params) 
    if @activity.save! 
     flash[:success] = 'Activity created successfully!' 
     redirect_to activities_display_path 
    else 
     flash[:error] = 'ERROR: Activity was not saved!' 
     #render_to_string #normally would have it render to the name of view ex: :new 
    end 
    end 

    def edit 
    @activity = Activity.find(params[:id]) 
    end 

    def update 
    @activity = Activity.update(activity_params) 
    if @activity.save 
     flash[:success] = 'Activity successfully updated!' 
     redirect_to root 
    else 
     flash[:error] = 'ERROR: Activity failed to update' 
     render_to_string 
    end 
    end 

    private 
    def activity_params 
     params.require(:activity).permit(:a_name) 
    end 

end 

編輯 - 17年3月10日 設法解決的上面的錯誤。原來,我需要在控制器的「顯示」操作中添加 「@activity = Activity.new」。之後,我將「@activity = Activity.all」更改爲「@activities = Activity.all」。 然後,在顯示視圖,我改變

<% @activity.each do |activity| %> 

<% @activities.each do |activity| %> 

和渲染是成功的。感謝所有的建議和幫助!

回答

0

作爲一個命名約定,我將在您的新建,編輯和更新操作及其相關視圖中將@activities重命名爲@activity。至於錯誤,form_for應該用於單個對象,而不是集合。它應該可能是這樣的:

<% @activities.each do |activity| %> 
     <div class = "row text-center"> 
     <div class = 'col-md-4'> 
      <h3> 
      <%= activity.a_name %> 
      </h3> 
     </div> 
     </div> 
    <% end %> 
+0

好的,謝謝你的建議!我相應地更新了控制器和視圖。不過,我仍然有相同的錯誤(NoMethodError),這是由於form_for在使用form_for的「_new」視圖內的form_for。我不知道如何解決這個問題,因爲我相信我需要form_for來創建使用用戶輸入模型的實例。 – VectorConvoy

+0

管理解決這個問題。謝謝你的建議 – VectorConvoy

相關問題