I am trying to add items to a table in rails but getting the error it the title. It works for me locally but on heroku it gives me this error. I have seen a couple of solution but none seem to work. This is my controller:
require './app/policies/personal_trainer_policy'
class PersonalTrainersController < ApplicationController
before_action :set_personal_trainer, only: [:show, :edit, :update, :destroy]
# GET /personal_trainers
# GET /personal_trainers.json
def index
@personal_trainers = PersonalTrainer.all
end
# GET /personal_trainers/1
# GET /personal_trainers/1.json
def show
end
# GET /personal_trainers/new
def new
@personal_trainer = PersonalTrainer.new
end
# GET /personal_trainers/1/edit
def edit
end
def personal_trainer_policy
@_personal_trainer_policy ||= PersonalTrainerPolicy.new(personal_trainer)
end
# POST /personal_trainers
# POST /personal_trainers.json
def create
@personal_trainer = PersonalTrainer.new(personal_trainer_params)
authorize @personal_trainer
if @personal_trainer.persist
render json: @personal_trainer.record
else
render json: @personal_trainer.errors, status: :unpocessably_entity
end
respond_to do |format|
if @personal_trainer.save
format.html { redirect_to @personal_trainer, notice: 'Personal trainer was successfully created.' }
format.json { render :show, status: :created, location: @personal_trainer }
else
format.html { render :new }
format.json { render json: @personal_trainer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /personal_trainers/1
# PATCH/PUT /personal_trainers/1.json
def update
authorize @personal_trainer
respond_to do |format|
if @personal_trainer.update(personal_trainer_params)
format.html { redirect_to @personal_trainer, notice: 'Personal trainer was successfully updated.' }
format.json { render :show, status: :ok, location: @personal_trainer }
else
format.html { render :edit }
format.json { render json: @personal_trainer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /personal_trainers/1
# DELETE /personal_trainers/1.json
def destroy
authorize @personal_trainer
@personal_trainer.destroy
respond_to do |format|
format.html { redirect_to personal_trainers_url, notice: 'Personal trainer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_personal_trainer
@personal_trainer = PersonalTrainer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def personal_trainer_params
params.require(:personal_trainer).permit(:firstName, :secondName, :desription, :amountOfClients)
end
end
The controller was generated using a scaffold. This is my erb file:
<p id="notice"><%= notice %></p>
<h1 class = "title">Personal Trainers</h1>
<div class = "page-conatiner">
<table class = "table">
<thead>
<%= stylesheet_link_tag 'gym_classes', media: 'all', 'data-turbolinks-track': 'reload' %>
<tr>
<th>First Name</th>
<th>Second Name</th>
<th>Desription</th>
<th>Amount of Clients</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @personal_trainers.each do |personal_trainer| %>
<tr>
<td><%= personal_trainer.firstName %></td>
<td><%= personal_trainer.secondName %></td>
<td><%= personal_trainer.desription %></td>
<td><%= personal_trainer.amountOfClients %></td>
<td><%= link_to 'Show', personal_trainer %></td>
<td><%= link_to 'Edit', edit_personal_trainer_path(personal_trainer) %></td>
<td><%= link_to 'Destroy', personal_trainer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.admin == true %>
<%= link_to 'New Personal Trainer', new_personal_trainer_path %>
<% end %>
</div>
and this is my model:
class PersonalTrainer < ApplicationRecord
has_many :gym_class_final
has_many :pt_client
end
I have it set so that admins can only create new items using a policy and adding authorise @personal_trainer. I wonder if this has something to do with the error. I just dont understand how it would work locally but not when its deployed. Any suggestions.
persist
is not a valid method on an active record object, you're looking for save
:
if @personal_trainer.save
render json: @personal_trainer.record
else
render json: @personal_trainer.errors, status: :unpocessably_entity
end
For more information on what methods are available for active record persistence, see https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html (Rails 6)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.