我有一个与AJAX有关的问题

时间:2019-08-26 04:16:45

标签: ruby ajax ruby-on-rails-5

  

创建后,我需要获取值,就在“ NEW”部分之下

下面是我打算做的图像,创建数据后,我希望在那里...

uesiton

phrases_term_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_terms.new(phrases_term_params)
    if @phrases_term.save
      redirect_to term_phrases_term_path(@term, @phrases_term), notice: "Phrases_Term was successfully created"
    else
      render "new"
    end
  end

  def show
    @phrases_term = PhrasesTerm.find(params[:id])
  end

  private

  def phrases_term_params
    params.require(:phrases_term).permit(:term_id, :phrase_id)
  end

  def set_term
    @term = Term.find(params[:term_id])
  end
end

查看 pussy_term

new.html.erb

<h1><%= I18n.t("pages.phrases_term.new.title") %></h1>
<%= render 'form', phrases_term: @phrases_term %>

_form.html.erb

<%= form_with scope: :phrases_term, url: term_phrases_terms_path, local: true do |form| %>
  <% if phrases_term.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(phrases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>

      <ul>
      <% phrases_term.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

   <div class="field">
    <%= form.label :term_id %> :
    <%= form.number_field :id %>
  </div>

  <div class="dropdown">
    <button class="dropbtn">Phrase</button>
    <div class="dropdown-content">
        <a><%= form.select :phrase_id, Phrase.all.collect { |p| [ p.id ] }, include_blank: true %></a>
    </div>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

条款:

_form.html.erb

<%= form_with(model: term, local: true) do |form| %>
  <% if term.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(term.errors.count, "error") %> prohibited this term from being saved:</h2>

      <ul>
      <% term.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :word %>
    <%= form.text_field :word %>
  </div>

  <div class="field">
    <%= form.label :meaning %>
    <%= form.text_field :meaning %>
  </div>

  <div class="field">
    <%= form.label :reading %>
    <%= form.text_field :reading %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

我不确定我是否已提供所有必要的信息,如果要添加更多信息,请在下面评论。 ---谢谢。

0 个答案:

没有答案