我正在尝试使用this tutorial向游戏模型添加新角色。它主要工作得很好,除了当我单击提交按钮我的表单时,它根本不会调用我的create.js.erb。我在该文件的顶部放置了一个警告('hello'),以便我知道它是否被调用,但事实并非如此。
其他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)
答案 0 :(得分:0)
您应该在控制器中添加:js格式。尝试改变
respond_to :html, :json
到
respond_to :html, :json, :js