我有术语和短语模型,并添加了嵌套资源。我想获取Phraes_term的术语ID以创建数据对
示例:术语1和短语2。 但是,当您按show.html.erb术语中的新按钮时,将会发生错误
-->error :PhrasesTermsController#new is missing a template for request formats: text/html
form.html.erb -(phrases_term)。
<%= form_with(model: phrases_term, local: true) do |form| %>
<% if pharases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(pharases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% pharases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :ID %>
<%= form.number_field_tag :ID %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
(phrases_term) -new.html.erb
<%= render 'form', phrases_term: @phrases_term %>
Phrases_terms_controller.rb
class PhrasesTermsController < ApplicationController
before_action :authenticate_user!
before_action :set_term
def new
@phrases_term = PhrasesTerm.new
end
def create
@phrases_term = @term.phrases_term
@phrases_term.user = current_user
@phrases_term.save
redirect_back(fallback_location: root_path)
end
private
def phrases_term_params
params.require(:phrases_term).permit(:term_id)
end
def set_term
@term = Term.find(params[:term_id])
end
end
routes.rb
resources :terms do
resources :phrases_terms, only: [:create, :destroy, :new]
end
(期限) Show.html.erb
<td><%= link_to 'New', new_term_phrases_term_path(@term) %></td>
(phrases_term).
_form.html.ebr
<%= form_with(model: phrases_term, local: true) do |form| %>
<% if pharases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(pharases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% pharases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :ID %>
<%= form.number_field_tag :ID %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
(phrases_term)
new.html.erb
<%= render 'form', phrases_term: @phrases_term %>
答案 0 :(得分:1)
这意味着您没有app / views / phrases_term(s)/new.html.erb文件。我不确定您是否已在'phrases_term'或'phrases_terms'下命名文件。
在正确的目录中创建一个,填充它,然后它将加载。