我有一个字典应用程序,其中word有很多定义,定义属于word。这是我的Word模型。
class Word < ApplicationRecord
belongs_to :user
has_many :definitions, dependent: :destroy
accepts_nested_attributes_for :definitions
self.per_page = 4
end
我的words_controller
class WordsController < ApplicationController
before_action :set_word, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /words
# GET /words.json
def index
@words = Word.all.order('created_at DESC').paginate(:page =>
params[:page])
end
# GET /words/1
# GET /words/1.json
def show
@definitions = @word.definitions.order(cached_votes_up: :desc,
created_at: :desc)
end
# GET /words/new
def new
@word = Word.new
end
def all_new
@word = Word.new
1.times { @word.definitions.build }
@words = Word.all
end
# GET /words/1/edit
def edit
end
# POST /words
# POST /words.json
def create
@word = Word.find_or_create_by(word: word_params[:word]) do |word|
word.definition.attributes = word_params[:definition_attributes]
end
@word.user_id = current_user.id
@word.definitions.each {|definition| definition.user_id =
current_user.id }
respond_to do |format|
if Word.where(word: word_params[:word]).present?
format.html { redirect_to root_path, notice: 'Word already exists.'
}
elsif @word.save
format.html { redirect_to root_path, notice: 'Word was successfully
created.' }
format.json { render :show, status: :created, location: @word }
else
format.html { render :new }
format.json { render json: @word.errors, status:
:unprocessable_entity }
end
end
end
# PATCH/PUT /words/1
# PATCH/PUT /words/1.json
def update
respond_to do |format|
if @word.update(word_params)
format.html { redirect_to root_path, notice: 'Word was successfully
updated.' }
format.json { render :show, status: :ok, location: @word }
else
format.html { render :edit }
format.json { render json: @word.errors, status:
:unprocessable_entity }
end
end
end
# DELETE /words/1
# DELETE /words/1.json
def destroy
@word.destroy
respond_to do |format|
format.html { redirect_to words_url, notice: 'Word was successfully
destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_word
@word = Word.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white
list through.
def word_params
params.require(:word).permit(:word, definitions_attributes:
[:definition, :URL])
end
end
如何重写此代码以检查该单词是否存在。如果没有,则创建单词或单词和定义(此部分有效);如果是,则仅创建现有单词的定义(此部分不起作用)。我运行了Rails.logger.debug,看起来块甚至都没有执行。显然,find_or_create_by不是这里使用的正确方法。
This is my form
<%= form_for @word do |form| %>
<% if word.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(word.errors.count, "error") %> prohibited this word
from being saved:</h2>
<ul>
<% word.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group upper-margin">
<%= form.text_field :word, id: :word_word, class: "form-control",
placeholder: "Word" %>
</div>
<%= form.fields_for :definitions do |definition| %>
<div class="form-group">
<button type="button" id="ShiftButton" class="btn btn-secondary form-
control" >I don't know the definition!</button>
<%= definition.text_field :definition, class: "form-control
definition", placeholder: "Definition", id: "chartdiv"%>
</div>
<% end %>
<div class="form-group">
<%= form.submit class: "btn btn-secondary form-control" %>
</div>
<% end %>
答案 0 :(得分:1)
@word = Word.find_or_create_by(word_params[:word])
@word.definitions.create(word_params[:definitions_attributes])
虽然你可能最终会得到重复的定义,因为你真的没有定义定义的方法&#34; unique&#34;