하나의 열이있는 Word 모델 : word가 있습니다. 제출 될 때 @word 객체를 만드는 양식이 있습니다.for 루프를 사용하여 복수의 객체를 동시에 생성
단어/_form.html.erb
<%= form_for(@word, :remote => (params[:action] == 'new' ? true : false)) do |f| %>
<fieldset>
<div class="field">
<%= f.text_field :word, :required => true %>
</div>
</fieldset>
<div class="actions">
<%= f.submit :disable_with => 'Submitting...' %>
</div>
<% end %>
단어/create.js.erb
$('#words').prepend('<%= escape_javascript(render @word) %>');
$('#new-word-form-container').find('input:not(:submit),select,textarea').val('');
내가 simontaniously 하나 개의 양식 제출에 여러 단어의 생성을 나타내는 표현하고 싶습니다 (즉, 각 개별 단어를 만들기 위해 다시 제출하는 대신).
문자열을 단어 배열 (쉼표 또는 공백으로 구분)로 분할하는 내 Word 모델의 메서드가 있습니다.
class Word < ActiveRecord::Base
attr_accessible :word
# Split Words method splits words seperated by a comma or space
def self.split_words(word)
# Determine if multiple words
if word.match(/[\s,]+/)
word = word.split(/[\s,]+/) # Split and return array of words
else
word = word.split # String => array
end
end
end
나는 각 배열 요소를 단계별 및 요소에 대한 @word 객체를 생성하는 내
create
행동에서 루프를 사용하는 것을 시도하고있다.
class WordsController < ApplicationController
respond_to :js, :json, :html
def create
split = Word.split_words(params[:word])
split.each do |w|
@word = Word.create(params[:w])
respond_with(@word)
end
end
아래에 나열된 바와 같이 현재 HashWithIndifferentAccess 오류가 발생합니다.
Started POST "/words" for 127.0.0.1 at 2014-06-10 13:09:26 -0400
Processing by WordsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0hOmyrQfFWHRkBt8hYs7zKuHjCwYhYdv444Zl+GWzEA=", "word"=>{"word"=>"stack, overflow"}, "commit"=>"Create Word"}
Completed 500 Internal Server Error in 0ms
NoMethodError (undefined method `match' for {"word"=>"stack, overflow"}:ActiveSupport::HashWithIndifferentAccess):
app/models/word.rb:9:in `split_words'
app/controllers/words_controller.rb:36:in `create'
모든 도움을 주시면 감사하겠습니다.
당신은 당신의 양식을 게시 할 수 있습니다. – DickieBoy
ActiveSupport :: HashWithIndifferentAccess 개체에 대해 일치 메서드를 사용할 수 없다고 생각하면 조건부 전에 표준 단어 루비 해시 개체로 단어 개체를 변환 할 수 있습니까? http://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html –
그래, 콘솔에서 해본 적이 있는데, w = word.to_hash와 같은 것을 할 수 있습니다. w.match ... –