У меня проблема в Rails 6. У меня есть 3 модели пользователя, персонажа и способности, которые связаны между собой: у пользователя может быть много персонажей, персонаж принадлежит пользователю, персонаж может иметь много способностей, способность принадлежит персонажу. Моя цель - добавить jquery и ajax, чтобы я мог создавать, изменять или удалять способности персонажей со страницы показа персонажей. Вот мой abilities_controller.rb:
class AbilitiesController < ApplicationController
def index
@abilities = Ability.all
end
def show
end
def new
@ability = Ability.new
end
def edit
end
def create
@character = Character.find(params[:character_id])
@ability = @character.ability.build(ability_params)
respond_to do |format|
if @ability.save
format.js
format.html { redirect_to @ability, notice: 'Ability was successfully created.' }
format.json { render :show, status: :created, location: @ability }
else
format.html { render :new }
format.json { render json: @ability.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @ability.update(ability_params)
format.html { redirect_to @ability, notice: 'Ability was successfully updated.' }
format.json { render :show, status: :ok, location: @ability }
else
format.html { render :edit }
format.json { render json: @ability.errors, status: :unprocessable_entity }
end
end
end
def destroy
@ability.destroy
respond_to do |format|
format.html { redirect_to abilities_url, notice: 'Ability was successfully destroyed.' }
format.json { head :no_content }
end
end
private
private
def ability_params
params.require(:ability).permit(:name, :value, :picture)
end
end
Страница показа персонажа:
<h1 align="center"><%= @character.name %></h1>
<hr />
<div class="row ">
<div class="col-md-2 offset-1 card-img-top card card-body bg-light mx-auto colsize" align="center">
<% if @character.image.attached? %>
<%= image_tag(@character.image, size: '120x120') %>
<% else %>
<small>No image</small>
<% end %>
</div>
<div class="col-md-8 card card-body bg-light mx-auto" >
<p>Listing character attributes:</p>
<div class="row ">
<% @character.abilities.each do |ability| %>
<div class="col-md-8 ">
<div class="card" style="width: 132px;">
<% if ability.picture.attached? %>
<%= image_tag(ability.picture, size: '130x130') %>
<% else %>
<small>No image</small>
<% end %>
<p class="mx-auto"><%= ability.name %>: <%= ability.value %></p>
<%= link_to 'Edit Ability', new_ability_path, remote: true, class:"btn btn-primary" %>
<%= link_to 'Delete Ability', new_ability_path, remote: true, class:"btn btn-primary" %>
</div>
</div>
<% end %>
</div>
<div id="new-ability" class="pt-2 pb-2">
<%= link_to 'New Ability', new_ability_path, remote: true, class:"btn btn-primary" %>
</div>
<hr />
<p>
<small>Created by: <%= @character.user.username.capitalize %>, <%= time_ago_in_words(@character.created_at) %> ago</small>
</p>
<div>
<tr>
<td><%= link_to 'Back', characters_path, class: "btn btn-primary" %></td>
<%if current_user && current_user == @character.user %>
<td><%= link_to 'Edit', edit_character_path(@character), class: "btn btn-warning" %></td>
<td><%= link_to "Delete this character", character_path(@character), method: :delete,
data: { confirm: "Are you sure you want to delete?" },
class: "btn btn-xs btn-danger" %>
</td>
<%end%>
</tr>
</div>
</div>
</div>
<div class="row">
<div class="col-md-11 card card-body bg-light mx-auto mt-3">
<% if @character.description %>
<p class="biography-title"><%=@character.name.capitalize%>'s Biography</p>
<p><%=@character.description%></p>
<%end%>
<div/>
</div>
Способности / просмотр / новые. js .erb
$('#new-ability a').hide().parent().append("<%= j render 'form', ability: @ability %>");
Способности /view/new.html.erb
<%= render 'form' %>
Способности / просмотр / создание. js .erb
$('new-ability form').remove();
$('new-ability a').show
Способности / просмотр / _form. html .erb
<%= form_with(model: ability) do |form| %>
<% if ability.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(ability.errors.count, "error") %> prohibited this ability from being saved:</h2>
<ul>
<% ability.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row mx-auto pt-2">
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :value %>
<%= form.text_field :value %>
</div>
<div class="field">
<%= form.label :picture %>
<%= form.file_field :picture %>
</div>
<div class="actions">
<%= form.submit %>
</div>
</div>
<% end %>
И для последних символов_controller.rb
class CharactersController < ApplicationController
before_action :authenticate_user!, except: [:show, :index]
before_action :set_character, only: [:show, :edit, :update, :destroy]
before_action :require_same_user, only: [:edit, :update, :destroy]
def index
@characters = Character.order(:name).page(params[:page]).per(3)
end
def show
end
def new
@character = Character.new
end
def edit
end
def create
@character = Character.new(character_params)
@character.user = current_user
if @character.save
flash[:success] = "Character was created successfully!"
redirect_to character_path(@character)
else
render 'new'
end
end
def update
if @character.update(character_params)
flash[:success] = "Character was updated successfully!"
redirect_to character_path(@character)
else
render 'edit'
end
end
def destroy
@character.destroy
flash[:danger] = "Character was deleted successfully"
redirect_to characters_path
end
private
def set_character
@character = Character.find(params[:id])
end
def character_params
params.require(:character).permit(:name, :image, :description, abilities_attributes: [:id, :_destroy, :character_id, :name, :value, :picture])
end
def require_same_user
if current_user != @character.user
redirect_to characters_path
end
end
end
Мой код опубликован c в git хранилище: https://github.com/djuks/rpg-game-editor/tree/ajax-devise_controllers-tests Если у кого-либо есть возможность Решение буду благодарен. Заранее спасибо