I'm trying to create an app, where a practitioner can add opening days and hours. The problem right now is, that when I hit the save button, it only creates one record. It should be creating a record for every day of the week.
So it would be
monday: open 9.00 - close 17.00
tuesday: open 8.30 - close 16.30
etc.
_form.html.erb
<script type="text/javascript">
$(document).ready(function () {
$('input:checkbox').bootstrapSwitch();
});
</script>
<%= #schedule.errors.full_messages %>
<%= simple_form_for([:clinic, :practitioner, #schedule]) do |f| %>
<table id="modal">
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<% Schedule.days.each do |day| %>
<tbody>
<tr>
<td id="modal-column-left" style="background-color:#aaa;">
<p>Åbningstider</p>
</td>
<td id="modal-column-right" style="background-color:#bbb;">
<p>Åbent/lukket</p>
</td>
</tr>
<tr>
<td id="modal-column-left" style="background-color:#aaa;">
<h2 style="margin-bottom: 5%;">
<h2 style="margin-bottom: 5%;"><%= day.titleize %>: <%= Date.today.send(day) %></h2>
<%= f.check_box :open_or_not, :data => { :size=>'small', 'on-color'=>'success', 'on-text'=>'YES', 'off-text'=>'NO' } %>
</td>
<td id="modal-column-right" style="background-color:#bbb;"><%= f.input :open_time %> <%= f.input :close_time %>
</td>
</tr>
</tbody>
<% end %>
</table>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
schedules_controller.rb
class SchedulesController < ApplicationController
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
# GET /schedules
# GET /schedules.json
def index
#schedules = Schedule.all
end
# GET /schedules/1
# GET /schedules/1.json
def show
end
# GET /schedules/new
def new
#schedule = Schedule.new
#schedules = Schedule.all
end
# GET /schedules/1/edit
def edit
end
# POST /schedules
# POST /schedules.json
def create
#schedule = Schedule.new(schedule_params)
respond_to do |format|
if #schedule.save
format.html { redirect_to clinic_practitioner_schedule_path(id: #schedule.id), notice: 'Schedule was successfully created.' }
format.json { render :show, status: :created, location: #schedule }
else
format.html { render :new }
format.json { render json: #schedule.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schedules/1
# PATCH/PUT /schedules/1.json
def update
respond_to do |format|
if #schedule.update(schedule_params)
format.html { redirect_to clinic_practitioner_schedule_path(#schedule), notice: 'Schedule was successfully updated.' }
format.json { render :show, status: :ok, location: #schedule }
else
format.html { render :edit }
format.json { render json: #schedule.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schedules/1
# DELETE /schedules/1.json
def destroy
#schedule.destroy
respond_to do |format|
format.html { redirect_to clinic_practitioner_schedules_url, notice: 'Schedule was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_schedule
#schedule = Schedule.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def schedule_params
params.require(:schedule).permit(:title, :start, :end, :practitioner_id, :account_id, :open_or_not, :day_of_week, :open_time, :close_time)
end
end
Read the documentation of complex forms.
You will want to attach multiple schedules to the Practitioner:
class Practitioner < ApplicationRecord
has_many :schedules
accepts_nested_attributes_for :schedules
end
The Controller
def practitioner_params
params.require(:practitioner).permit(:name, schedule_attributes: [:id, :open_or_not, :day_of_week, :open_time, :close_time])
end
And in the form:
<%= simple_form_for #practitioner do |f| %>
<%= f.input :name %>
<%= f.simple_fields_for :schedules do |s| %>
<%= s.input :open_or_not %>
<%= s.input :day_of_week %>
<%= s.input :open_time %>
<%= s.input :close_time %>
<% end %>
<% end %>
To dynamically add rows use the Cocoon gem.
Related
I have a Ruby on Rails app that handles a users income, decides if it is an allowance or an income and applies the appropriate tax rates. I have included some enums to do basic functions as outlined below.
When I go to update from the default I hit the issue '1' is not a valid incomeType.
Below you can see the set up including model, controller and form.
model :
class Income < ApplicationRecord
enum incomeType: {income: 0, allowance: 1 }
enum taxed: {yes: 0, no: 1 }
belongs_to :user
end
controller:
class IncomesController < ApplicationController
before_action :set_income, only: [:show, :edit, :update, :destroy]
# GET /incomes
# GET /incomes.json
def index
#incomes = current_user.incomes.all
end
# GET /incomes/1
# GET /incomes/1.json
def show
end
# GET /incomes/new
def new
#income = current_user.incomes.build
end
# GET /incomes/1/edit
def edit
end
# POST /incomes
# POST /incomes.json
def create
#income = current_user.incomes.new(income_params)
respond_to do |format|
if #income.save
format.html { redirect_to #income, notice: 'Income was successfully created.' }
format.json { render :show, status: :created, location: #income }
else
format.html { render :new }
format.json { render json: #income.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /incomes/1
# PATCH/PUT /incomes/1.json
def update
respond_to do |format|
if #income.update(income_params)
format.html { redirect_to #income, notice: 'Income was successfully updated.' }
format.json { render :show, status: :ok, location: #income }
else
format.html { render :edit }
format.json { render json: #income.errors, status: :unprocessable_entity }
end
end
end
# DELETE /incomes/1
# DELETE /incomes/1.json
def destroy
#income.destroy
respond_to do |format|
format.html { redirect_to incomes_url, notice: 'Income was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_income
#income = Income.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def income_params
params.require(:income).permit(:amount, :frequency, :user_id, :incomeType, :country, :taxed)
end
end
Form :
<%= form_with(model: income, local: true) do |form| %>
<% if income.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(income.errors.count, "error") %> prohibited this income from being saved:</h2>
<ul>
<% income.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :amount %>
<%= form.text_field :amount, id: :income_amount %>
</div>
<div class="field">
<%= form.label :frequency %>
<%= form.select :frequency, options_for_select([['Weekly', '52'], ['Fortnightly', '26'], ['Monthly', '12'], ['Bi-Monthly', '6'], ['Annually', '1']]), id: :income_frequency %>
</div>
<div class="field">
<%= form.label :incomeType %>
<%= form.select :incomeType, options_for_select([['Income', '0'], ['Allowance', '1']]), id: :incomeType %>
</div>
<div class="field">
<%= form.label :taxed %>
<%= form.select :taxed, options_for_select([['Yes', '0'], ['No', '1']]), id: :taxed %>ra
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Hopefully you can point me in the right direct.
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"HUTd5Bav9eAfWPTFFyqeD69aL4mIgZodIuL1+9eL0zIhN+SjDwAMcD7AzKEuhm6az4iALBSrDUXd/1vVfN77SQ==",
"income"=>{"amount"=>"102000.0", "frequency"=>"1", "incomeType"=>"0", "taxed"=>"1"},
"commit"=>"Update Income",
"id"=>"4f9fc439-4578-487e-bc5d-02cf0cd9aaa3"}
Thanks in advance for your help
Yes it is because enum needs key not the value and you are sending value not the key so in your case enum is trying to find '0' as one key which is not present. So just change your form slightly
<%= form.select :incomeType, options_for_select([['Income', 'income'], ['Allowance', 'allowance']]), id: :incomeType %>
PS : change form for taxed as well.
Hope this will help
The Remove Avatar checkbox isn't doing anything for me on my edit form. Here is my code.
_form.html.erb
<%= simple_form_for(#child, html: { multipart: true}) do |f| %>
<% if child.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(child.errors.count, "error") %> prohibited this child from being saved:</h2>
<ul>
<% child.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :name %><br />
<div class="field"><%= f.text_field(:name) %> <br /></div>
<%= f.label :balance %><br />
<div class="field"> <%= f.text_field(:balance) %> <br /></div>
<%= f.label :parent_id %><b> ID</b><br />
<%= f.number_field :parent_id, :value => current_parent.id, :readonly => true %><br />
<%=f.label :avatar %><br />
<%= image_tag(#child.avatar_url) if #child.avatar? %>
<%= f.file_field(:avatar) %>
<%= f.hidden_field(:avatar_cache) %>
<br />
<label>
<%= f.check_box :remove_avatar %>
Remove Avatar
</label>
<div class="actions">
<% if #child.new_record? == true %>
<%= f.submit("Add Child") %>
<% else %>
<%= f.submit("Save Child") %>
<% end %>
</div>
<% end %>
Child Controller
class ChildrenController < ApplicationController
before_filter :authenticate_parent!
before_action :set_child, only: [:show, :edit, :update, :destroy]
# GET /children
# GET /children.json
def index
#children = Child.all
end
# GET /children/1
# GET /children/1.json
def show
end
# GET /children/new
def new
#child = Child.new
end
# GET /children/1/edit
def edit
end
# POST /children
# POST /children.json
def create
#child = Child.new(child_params)
if #child.avatar.file.nil?
img = LetterAvatar.generate(#child.name, 200)
File.open(img) do |f|
#child.avatar = f
end
end
respond_to do |format|
if #child.save
format.html { redirect_to #child, notice: 'Child was successfully created.' }
format.json { render :show, status: :created, location: #child }
else
format.html { render :new }
format.json { render json: #child.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /children/1
# PATCH/PUT /children/1.json
def update
if #child.avatar.file.nil?
img = LetterAvatar.generate(#child.name, 200)
File.open(img) do |f|
#child.avatar = f
end
end
respond_to do |format|
if #child.update(child_params)
format.html { redirect_to #child, notice: 'Child was successfully updated.' }
format.json { render :show, status: :ok, location: #child }
else
format.html { render :edit }
format.json { render json: #child.errors, status: :unprocessable_entity }
end
end
end
# DELETE /children/1
# DELETE /children/1.json
def destroy
#child.destroy
respond_to do |format|
format.html { redirect_to children_url, notice: 'Child was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_child
#child = Child.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def child_params
params.fetch(:child, {}).permit(:name, :balance, :parent_id, :avatar)
# params.fetch(:user, {}).permit(:first_name, :last_name, :email, :phone,
end
end
If I click save with the Remove Avatar box checked, it literally does nothing to the avatar, if I go back to any page that displayed the avatar, it is still there. What am I doing wrong? I am using Ruby on Rails 5.0.1
Side note: I am aware of some of the deprecated things that are being used here, such as before_filter. I am working with a large group of people so not all of this code is mine, fixing it is not priority at the moment.
Hello I have a nested database "processing" which belongst to "shopping_process". However I want somehow to see the messages, which are saved in processing. How can I do that? If I go to http://localhost:3000/processings/index the Error: Couldn't find Processing with 'id'=index appears. Has somebody an idea?
processings_controller.rb
class ProcessingsController < ApplicationController
before_action :set_processing, only: [:show, :edit, :update, :destroy]
# GET /processings
# GET /processings.json
def index
#processings = Processing.all
end
# GET /processings/1
# GET /processings/1.json
def show
end
# GET /processings/new
def new
#processing = Processing.new
end
# GET /processings/1/edit
def edit
end
# POST /processings
# POST /processings.json
def create
#processing = Processing.new(processing_params)
respond_to do |format|
if #processing.save
format.html { redirect_to #processing, notice: 'Processing was successfully created.' }
format.json { render :show, status: :created, location: #processing }
else
format.html { render :new }
format.json { render json: #processing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /processings/1
# PATCH/PUT /processings/1.json
def update
respond_to do |format|
if #processing.update(processing_params)
format.html { redirect_to #processing, notice: 'Processing was successfully updated.' }
format.json { render :show, status: :ok, location: #processing }
else
format.html { render :edit }
format.json { render json: #processing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /processings/1
# DELETE /processings/1.json
def destroy
#processing.destroy
respond_to do |format|
format.html { redirect_to processings_url, notice: 'Processing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_processing
#processing = Processing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def processing_params
params.require(:processing).permit(:shopping_process_id, :shopping_list_id, :accepted, :responded, :message)
end
end
view/processings/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Processings</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #Processing.all.each do |processing| %>
<tr>
<td><%= link_to 'Show', processing %></td>
<td><%= link_to 'Edit', edit_processing_path(processing) %></td>
<td><%= link_to 'Destroy', processing, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Processing', new_processing_path %>
models/processing.rb
class Processing < ApplicationRecord
# db associations
belongs_to :shopping_process
belongs_to :shopping_list
# validations
validates :shopping_list, :presence => true
end
Now here comes the code for the database "shopping_processes"
shopping_processes_controller.rb
class ShoppingProcessesController < ApplicationController
load_and_authorize_resource
layout 'shopping_process', only: [:shopper_show, :senior_show]
# GET /shopping_processes/1
# GET /shopping_processes/1.json
def show
end
# POST /shopping_processes
# POST /shopping_processes.json
def create
#shopping_process.status =nil
#shopping_process.processed = nil
puts params.inspect
respond_to do |format|
if #shopping_process.save
format.html { redirect_to #shopping_process, notice: 'Shopping process was successfully created.' }
format.json { render :show, status: :created, location: #shopping_process }
else
format.html { render :new }
format.json { render json: #shopping_process.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /shopping_processes/1
# PATCH/PUT /shopping_processes/1.json
def update
respond_to do |format|
if #shopping_process.update(shopping_process_params)
format.html { redirect_to #shopping_process, notice: 'Shopping process was successfully updated.' }
format.json { render :show, status: :ok, location: #shopping_process }
else
format.html { render :edit }
format.json { render json: #shopping_process.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shopping_processes/1
# DELETE /shopping_processes/1.json
def destroy
#shopping_process.destroy
respond_to do |format|
format.html { redirect_to shopping_processes_url, notice: 'Shopping process was successfully destroyed.' }
format.json { head :no_content }
end
end
def shopper_show
#shopping_process = ShoppingProcess.find(params[:id])
#users = {}
first = true
#shopping_process.shopping_lists.each do |shopping_list|
user = shopping_list.user
#users[user.id] = {color: (first ? 'blue' : 'yellow'), name: user.firstname + ' ' + user.lastname}
first = false
end
end
def senior_show
#shopping_process = ShoppingProcess.find(params[:id])
#shopping_process.shopping_lists.each do |shopping_list|
#if shopping_list.user == current_user
# #shopping_list = shopping_list
#end
#shopping_list = shopping_list
#to show the correct shopping list uncomment the block above and remove the declartion underneath it
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_shopping_process
#shopping_process = ShoppingProcess.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shopping_process_params
params.require(:shopping_process).permit(:user_id, :status, :appointed, :processed, :shopping_list_id, processings_attributes: [ :shopping_list_id, :message ])
end
end
views/shopping_processes/form.html.erb
<%= form_for(shopping_process) do |f| %>
<% if shopping_process.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(shopping_process.errors.count, "error") %> prohibited this shopping_process from being saved:</h2>
<ul>
<% shopping_process.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :appointed %>
<%= f.datetime_select :appointed %>
</div>
<div class="field">
<%= f.label :shopper %>
<%= f.text_field :user_id, value: current_user.id, :readonly => true%>
</div>
<div class="field">
<%= f.label :shopping_list_id %>
<%= f.select :shopping_list_id, ShoppingList.accessible_by(current_ability).map{ |sl| [sl.name, sl.id] } %>
</div>
<!-- SUB FORM FOR NESTED RESOURCE : PROCESSINGS -->
<%= f.fields_for :processings, #shopping_process.processings.build do |ff| %>
<div>
<%= ff.label :Begleiter %>
<%= ff.select :shopping_list_id, ShoppingList.all.map{ |sl| [sl.user.firstname, sl.id]} , {:prompt => true} %>
</div>
<div>
<%= ff.label :message %>
<%= ff.text_field :message%>
</div>
<% end %>
<!-- Submit-->
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I'm trying to create a has-many association within a ruby on rails app where a user has-many goals and a goal has-many steps
I can't seem to figure out how to link the creation of a Step for a certain Goal. I've been playing around with it for a while and looking around on here but haven't found a solution.
Below are my Goal_controller, Step_Controller, Step form, and Goal form
Goal Controller:
class GoalsController < ApplicationController
before_action :set_goal, only: [:show, :edit, :update, :destroy]
before_filter :authorize
# GET /goals
# GET /goals.json
def index
#goals = Goal.all
end
# GET /goals/1
# GET /goals/1.json
def show
#goal = Goal.find(params[:id])
session[:current_goal] = #goal.id
end
# GET /goals/new
def new
#goal = Goal.new
end
# GET /goals/1/edit
def edit
end
# POST /goals
# POST /goals.json
def create
#goal = current_user.goals.new(goal_params)
respond_to do |format|
if #goal.save
format.html { redirect_to #goal, notice: 'Goal was successfully created.' }
format.json { render :show, status: :created, location: #goal }
else
format.html { render :new }
format.json { render json: #goal.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /goals/1
# PATCH/PUT /goals/1.json
def update
respond_to do |format|
if #goal.update(goal_params)
format.html { redirect_to #goal, notice: 'Goal was successfully updated.' }
format.json { render :show, status: :ok, location: #goal }
else
format.html { render :edit }
format.json { render json: #goal.errors, status: :unprocessable_entity }
end
end
end
# DELETE /goals/1
# DELETE /goals/1.json
def destroy
#goal.destroy
respond_to do |format|
format.html { redirect_to goals_url, notice: 'Goal was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_goal
#goal = Goal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def goal_params
params.require(:goal).permit(:Goal, :Description, :Date, :DueDate, :user_id)
end
end
Step Controller:
class StepsController < ApplicationController
#before_action :set_step, only: [:show, :edit, :update, :destroy]
before_filter :authorize
# GET /steps
# GET /steps.json
def index
#steps = Goal.find(params[:goal_id]).steps.all
end
def new
#step = Goal.find(params[:goal_id]).steps.new
end
# GET /steps/1
# GET /steps/1.json
def show
end
# GET /steps/1/edit
def edit
end
def create
#step = Goal.find(params[:goal_id]).steps.new(step_params)
respond_to do |format|
if #step.save
format.html { redirect_to #step, notice: 'Step was successfully created.' }
format.json { render :show, status: :created, location: #step }
else
format.html { render :new }
format.json { render json: #step.errors, status: :unprocessable_entity }
end
end
redirect_to(goal_steps_url(#goal))
end
def update
#step.update(step_params)
respond_to do |format|
if #step.update(step_params)
format.html { redirect_to #step, notice: 'Step was successfully updated.' }
format.json { render :show, status: :ok, location: #step }
else
format.html { render :edit }
format.json { render json: #step.errors, status: :unprocessable_entity }
end
end
end
# POST /steps
# POST /steps.json
def destroy
#step.destroy
respond_to do |format|
format.html { redirect_to steps_url, notice: 'Step was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_step
#step = Goal.find(params[:goal_id]).Step.find(params[:id])
end
def step_params
params.require(:step).permit(:requirement, :completionTime, :goal_id)
end
end
Step Form:
<%= form_for(#step) do |f| %>
<% if #step.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#step.errors.count, "error") %> prohibited this step from being saved:</h2>
<ul>
<% #step.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :requirement %><br>
<%= f.text_field :requirement %>
</div>
<div class="field">
<%= f.label :completionTime %><br>
<%= f.number_field :completionTime %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Goal Form:
<%= form_for(#goal) do |f| %>
<% if #goal.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#goal.errors.count, "error") %> prohibited this goal from being saved:</h2>
<ul>
<% #goal.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :Goal %><br>
<%= f.text_field :Goal %>
</div>
<div class="field">
<%= f.label :Description %><br>
<%= f.text_area :Description %>
</div>
<div class="field">
<%= f.label :Date %><br>
<%= f.date_select :Date %>
</div>
<div class="field">
<%= f.label :DueDate %><br>
<%= f.date_select :DueDate %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
It looks like you will be missing your goal_id when you submit your step creation form. You will need to store it either in a hidden field in your step form, or as part of the route (e.g. POST /goals/10/steps).
I have this code below which loads the patient data and with each one the update button updates it but on clicking nothing happens, here is the code:
<% emergency_case.patients.each do |patient| %>
<tr>
<%= simple_form_for (:patient),url: patients_edit_path(patient.id) do |f|%>
<td><%=f.input :name ,:input_html => { :value => patient.name},label: false %></td>
<td><%=f.input :IDNumber ,:input_html => { :value => patient.IDNumber},label: false %></td>
<td><%=f.input :age ,:input_html => { :value => patient.age},label: false %></td>
<td><%=f.input :phone ,:input_html => { :value => patient.phone},label: false %></td>
<td><%=f.input :address ,:input_html => { :value => patient.address},label: false %></td>
<td><%=f.input :injury ,:input_html => { :value => patient.injury},label: false %></td>
<td><%= f.collection_select(:state_id, State.all, :id, :state) %></td>
<td><%= f.collection_select(:Act, Act.all, :id, :act) %></td>
<td><%=f.submit %></td>
<% end %>
</tr>
<% end %>
Here is the paitent controller which am sending the form for to make updates on the paitent that is sent:
class PatientsController < ApplicationController
before_action :set_patient, only: [:show, :edit, :update, :destroy]
# GET /patients
# GET /patients.json
def index
#patients = Patient.all
end
# GET /patients/1
# GET /patients/1.json
def show
end
# GET /patients/new
def new
#patient = Patient.new
end
# GET /patients/1/edit
def edit
#patient =Patient.find(params[:id])
end
# POST /patients
# POST /patients.json
def create
#patient = Patient.new(patient_params)
respond_to do |format|
if #patient.save
format.html { redirect_to #patient, notice: 'Patient was successfully created.' }
format.json { render :show, status: :created, location: #patient }
else
format.html { render :new }
format.json { render json: #patient.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /patients/1
# PATCH/PUT /patients/1.json
def update
respond_to do |format|
if #patient.update(patient_params)
format.html { redirect_to #patient, notice: 'Patient was successfully updated.' }
format.json { render :show, status: :ok, location: #patient }
else
format.html { render :edit }
format.json { render json: #patient.errors, status: :unprocessable_entity }
end
end
end
# DELETE /patients/1
# DELETE /patients/1.json
def destroy
#patient.destroy
respond_to do |format|
format.html { redirect_to patients_url, notice: 'Patient was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_patient
#patient = Patient.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def patient_params
params.require(:patient).permit(:name, :isDead, :status, :IDNumber, :emergency_case_id,:state_id,:address,:age,:phone,:injury,:act)
end
end
Several issues to contend with here:
<% emergency_case.patients.each do |patient| %>
<%= content_tag :tr do %>
<%= simple_form_for patient, method: :put do |f|%>
<% attributes = %i(name IDNumber age phone address injury) %>
<% patient.attributes do |attr| %>
<%= content_tag :td, f.input attr, input_html: { value: patient.send(attr)}, label: false %>
<% end %>
<%= content_tag :td, f.state_select :state_id %>
<%= content_tag :td, f.collection_select(:Act, Act.all, :id, :act) %>
<%= content_tag :td, f.submit %>
<% end %>
<% end %>
<% end %>
ALWAYS use snake_case for attributes (IDNumber is baaad umkay)
Check out the state_select gem
Loops are the BEST way to keep forms succinct & efficient
Your form is sending to the edit action -- you need to send to the update action
#4 will answer your question -- patients_edit_path(patient.id)
What you need is to send to the update path: patient_path(patient), method: :put... or simply: patient, method: :put
let simple form do the work for you on the url, method, etc unless you have something that is custom. If this doesn't work please post more info on the error you are getting in your post.
<% emergency_case.patients.each do |patient| %>
<tr>
<%= simple_form_for patient do |f|%>
....#form stuff
<td><%=f.submit %></td>
<% end %>
</tr>
<% end %>