私が理解しているところでは、レンダーを使用してビュー内にビューを持つことができます。私がそれを使用しようとしてきたのは表示ページを持つことで、このページにはいくつかのボタンがあります。 1つのボタンは、アクティビティを作成するためのフォームを表示/非表示にします。本質的に、ページはアクティビティを表示し、対応するボタンをクリックしてフォームを記入することにより、より多くを作成することを可能にする。Rails - 別のアクションのビューをレンダリングしますか?
私は「新しい」アクションに対応する「新しい」ページをレンダリングしようとしてきたが、私は次の問題
NoMethodError活動中#表示
未定義のメソッド `to_key」内に実行しました 活動:: ActiveRecord_Relationのために:0x694e270
は、このエラーにアップ検索、私はそれが私の活動コントローラ内の「表示」アクションを必要とすると思うし、それが「form_を期待していませんかために"。私はこの問題を解決する方法、またはレンダリングを正しく使用しているかどうかわかりません。
私が見てきた例が、HTML間で共有要素がある場合にレンダリングを利用しているように、レンダリングを使用すると、コードを減らすことができます。表示ページは、アクティビティを作成するためのフォームを持つ唯一のページになるので、単にhtmlコードを追加するだけです。
ありがとうございます、ありがとうございます!
Activities画面のHTML:HTML _new
<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>
アクティビティ:
<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
EDIT - 修正するマネージド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