내가 이해 한대로 렌더링을 사용하여보기 내에서보기를 가질 수 있습니다. 내가 사용하고자하는 것은 디스플레이 페이지를 가지고이 페이지에 몇 개의 버튼이 있습니다. 하나의 버튼은 액티비티를 생성하기위한 폼을 표시하거나 숨길 수 있습니다. 본질적으로, 페이지는 활동을 표시하고 사용자가 해당 버튼을 클릭하고 양식을 채워 더 많은 것을 만들 수있게합니다.레일 - 다른 동작으로 뷰 렌더링?
나는 "새로운"작업에 해당 "새로운"페이지를 렌더링하는 시도하지만 다음 문제
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>
활동 HTML _new :
<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
편집 -를 해결하기 위해 관리 03/10/17 오류가 발생했습니다. 밝혀졌습니다 컨트롤러의 "표시"동작에 "@activity = Activity.new"를 추가해야했습니다. 그 후, "@activity = Activity.all"을 "@activities = Activity.all"로 변경했습니다. 그런 다음 화면보기에, 나는
<% @activities.each do |activity| %>
에
<% @activity.each do |activity| %>
을 변경하고 성공했습니다 렌더링. 모든 조언과 도움에 감사드립니다!
좋습니다, 조언 해 주셔서 감사합니다! 컨트롤러와 뷰를 적절하게 업데이트했습니다. 그러나 나는 여전히 form_for를 사용하는 "_new"뷰 내에서 form_for로 인해 발생하는 동일한 오류 (NoMethodError)를 가지고 있습니다. 사용자 입력을 사용하여 모델의 인스턴스를 만들려면 form_for가 필요하다고 생각하므로이 문제를 해결하는 방법을 모르겠습니다. – VectorConvoy
관리 문제 해결. 조언 감사합니다 – VectorConvoy