从我的理解,你可以使用渲染在视图中有一个视图。我一直试图使用它的地方是有一个显示页面,并且在这个页面上有一些按钮。一个按钮可以显示/隐藏创建活动的表单。本质上,页面将显示活动并允许用户通过单击相应的按钮并填写表单来创建更多活动。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">←</span>Previous </a></li>
<li class="next"><a href="#"> Next<span aria-hidden="true">→</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| %>
和渲染是成功的。感谢所有的建议和帮助!
好的,谢谢你的建议!我相应地更新了控制器和视图。不过,我仍然有相同的错误(NoMethodError),这是由于form_for在使用form_for的“_new”视图内的form_for。我不知道如何解决这个问题,因为我相信我需要form_for来创建使用用户输入模型的实例。 – VectorConvoy
管理解决这个问题。谢谢你的建议 – VectorConvoy