ruby-on-rails - 缺少请求格式 : text/html 的模板

标签 ruby-on-rails ruby

I have Term and Phrase models and I am adding Nested Resources. I want to get the Term Id for Phraes_term to create a data pair

Example: Term 1 and Phrase 2. But when you press the new button from show.html.erb term, an error will occur

-->error :PhrasesTermsController#new is missing a template for request formats: text/html
表单.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

路由文件
resources :terms do
    resources :phrases_terms, only: [:create, :destroy, :new]
  end
(学期)
显示.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 %>

最佳答案

这意味着您没有 app/views/phrases_term(s)/new.html.erb 文件。我不确定您是否已将文件命名为“phrases_term”或“phrases_terms”。

在正确的目录中创建一个,填充它,它将加载。

关于ruby-on-rails - 缺少请求格式 : text/html 的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57569825/

相关文章:

ruby - 仅匹配Elasticsearch中字符串的第一个字母

java - 为什么在 java 类中包含类成员变量不像 ruby​​ mixin 那样?

ruby - OpenSSL::SSL::SSLErrorWaitReadable 读取会阻塞

ruby-on-rails - 如何在 Rails 模型中设置人类可读的属性名称?

ruby-on-rails - heroku 上的 bitbucket 私有(private)存储库

ruby-on-rails - 特定时间的状态机转换

ruby-on-rails - 跟踪谁在 Rails 中做了什么?

ruby-on-rails - 无法在 Windows 7 x64 上安装回形针 2.5.0

mysql - 合并一年来创建的具有相同结构的两个数据库,同时牢记 ID 和关系 - MySQL

ruby - 为什么在 Ruby 对象上调用 "p"调用 to_s 然后输出非字符串?