0
我正在嘗試使用this tutorial將新的角色添加到遊戲模型中。它大多工作得很好,除了當我點擊提交按鈕我的表單時,它根本不會調用我的create.js.erb。我在該文件的頂部放了一個警告('hello'),以便我知道它是否被調用,事實並非如此。js.erb未被觸發
其他js片段,如new.js.erb(呈現表單)和destroy.js.erb(從字符列表中刪除已銷燬的字符)正常工作。
這裏是我的代碼:
查看:
#bottom of views/characters/index.html.erb
<div>
<%= link_to "Add Character", new_play_character_path(@play), remote: true %>
</div>
<div id="character-form" style="display:none;"></div>
形式:
<%= bootstrap_form_for([@play, @character], inline_errors: false, remote: true, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10") do |f| %>
<%= f.text_field :name %>
<%= f.select :age, ["Baby","Child","Teen","Young Adult","Adult","Senior"] %>
<%= f.select :gender, ["Male","Female","Neutral"] %>
<%= f.submit %>
<% end %>
控制器(相關位):
class CharactersController < ApplicationController
before_action :set_character, only: [:show, :edit, :update, :destroy]
before_action :set_play
respond_to :html, :json
def index
@characters = Character.where("play_id = #{@play.id}")
end
def new
@character = Character.new
end
def create
@character = @play.characters.build(character_params)
flash[:notice] = "Character was successfully created." if @character.save
respond_with(@play)
end
create.js.erb
alert('hello');
$('#characters').html("<%= j render(@play.characters) %>");
$('#character-form').slideUp(350);
最後,我的日誌:
Started POST "/plays/5/characters" for 127.0.0.1 at 2016-07-06 14:22:03 -0400
Processing by CharactersController#create as JS
Parameters: {"utf8"=>"✓", "character"=>{"name"=>"Romeo", "age"=>"Teen", "gender"=>"Male"}, "commit"=>"Create Character", "play_id"=>"5"}
Play Load (0.1ms) SELECT `plays`.* FROM `plays` INNER JOIN `authors` ON `authors`.`id` = `plays`.`author_id` WHERE `plays`.`id` = 5 ORDER BY authors.last_name LIMIT 1
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `characters` (`name`, `age`, `gender`, `play_id`, `created_at`, `updated_at`) VALUES ('Romeo', 'Teen', 'Male', 5, '2016-07-06 18:22:03', '2016-07-06 18:22:03')
(87.1ms) COMMIT
Redirected to http://localhost:3000/plays/5
Completed 302 Found in 94ms (ActiveRecord: 87.5ms)
Started GET "/plays/5" for 127.0.0.1 at 2016-07-06 14:22:03 -0400
Processing by PlaysController#show as JS
Parameters: {"id"=>"5"}
Play Load (0.1ms) SELECT `plays`.* FROM `plays` INNER JOIN `authors` ON `authors`.`id` = `plays`.`author_id` WHERE `plays`.`id` = 5 ORDER BY authors.last_name LIMIT 1
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
CACHE (0.0ms) SELECT `plays`.* FROM `plays` INNER JOIN `authors` ON `authors`.`id` = `plays`.`author_id` WHERE `plays`.`id` = 5 ORDER BY authors.last_name LIMIT 1 [["id", "5"]]
Author Load (0.1ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 4 ORDER BY `authors`.`last_name` ASC LIMIT 1
Act Load (0.1ms) SELECT `acts`.* FROM `acts` WHERE `acts`.`play_id` = 5 ORDER BY `acts`.`act_number` ASC
Character Load (0.4ms) SELECT `characters`.* FROM `characters` WHERE `characters`.`play_id` = 5 ORDER BY name
Rendered characters/_character.html.erb (3.3ms)
Rendered plays/show.html.erb within layouts/application (8.6ms)
Completed 200 OK in 662ms (Views: 654.6ms | ActiveRecord: 0.9ms)
賓果!謝謝!我覺得我在其他一些地方嘗試過,但遇到了另一個阻塞的問題,並且在修復了其他錯誤之後沒有再嘗試它。 – nilatti