Ruby on Rails Sunday, February 8, 2015

(Apologies in advance...I can't seem to format this post
correctly...before answering, please let me know how to force
newlines/format code!!)

I've seen this error often and have tried to get to understand it based
on other posts, but to no avail.


I'm very new to Rails and so my lack of knowledge of the theoretical
framework here is likely the cause of this pitfall, but I would
absolutely love it if someone could please lead me up to what exactly
needs to change to avoid the error:


The exact error is:


Application(#70358560838160) expected, got String(#18246440)


Extracted source (around line #27):


> # POST /applications.json
> def create
> @application = Application.new(application_params)
>
> respond_to do |format|
> if @application.save


The parameters are as follows:

> {"utf8"=>"✓",
>
"authenticity_token"=>"wqSTW1Blj7c7sdlx1VCuvxDxqaCeT3FnaHa3yk/98NF7tym4YOc+9EMxlagj5/cgwCa3ZyjLsIxKJG/jGVCV1w==",
> "application"=>{"patients_ptID"=>"1",
> "pharm_manufacturers_phID"=>"1",
> "medications_rxnorm_ndc"=>"Tylenol",
> "app_status"=>"Pending",
> "date_init(1i)"=>"2015",
> "date_init(2i)"=>"2",
> "date_init(3i)"=>"8"},
> "commit"=>"Create Application"}


Here is my ApplicationsController (applications_controller.rb):

class ApplicationsController < ApplicationController
before_action :set_application, only: [:show, :edit, :update,
:destroy]

# GET /applications
# GET /applications.json
def index
@applications = Application.all
end

# GET /applications/1
# GET /applications/1.json
def show
end

# GET /applications/new
def new
@application = Application.new
end

# GET /applications/1/edit
def edit
end

# POST /applications
# POST /applications.json
def create
@application = Application.new(application_params)

respond_to do |format|
if @application.save
format.html { redirect_to @application, notice: 'Application was
successfully created.' }
format.json { render :show, status: :created, location:
@application }
else
format.html { render :new }
format.json { render json: @application.errors, status:
:unprocessable_entity }
end
end
end

# PATCH/PUT /applications/1
# PATCH/PUT /applications/1.json
def update
respond_to do |format|
if @application.update(application_params)
format.html { redirect_to @application, notice: 'Application was
successfully updated.' }
format.json { render :show, status: :ok, location: @application
}
else
format.html { render :edit }
format.json { render json: @application.errors, status:
:unprocessable_entity }
end
end
end

# DELETE /applications/1
# DELETE /applications/1.json
def destroy
@application.destroy
respond_to do |format|
format.html { redirect_to applications_url, notice: 'Application
was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between
actions.
def set_application
@application = Application.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the
white list through.
def application_params
params.require(:application).permit(:patients_ptID,
:pharm_manufacturers_phID, :medications_rxnorm_ndc, :app_status,
:date_init)
end
end

And my applications form (applications/_form.html.erb):

<%= form_for(@application) do |f| %>
<% if @application.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@application.errors.count, "error") %>
prohibited this application from being saved:</h2>

<ul>
<% @application.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :patients_ptID %><br>
<%= f.number_field :patients_ptID %>
</div>
<div class="field">
<%= f.label :pharm_manufacturers_phID %><br>
<%= f.number_field :pharm_manufacturers_phID %>
</div>
<div class="field">
<%= f.label :medications_rxnorm_ndc %><br>
<%= f.text_field :medications_rxnorm_ndc %>
</div>
<div class="field">
<%= f.label :app_status %><br>
<%= f.text_field :app_status %>
</div>
<div class="field">
<%= f.label :date_init %><br>
<%= f.date_select :date_init %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

And patient.rb:

class Patient < ActiveRecord::Base
belongs_to :application, foreign_key: "ptID", class_name:
"Application"
belongs_to :dispensed_meds, foreign_key: "ptID", class_name:
"DispensedMed"
belongs_to :prescriptions, foreign_key: "ptID", class_name:
"Prescription"
end

And application.rb:

class Application < ActiveRecord::Base
has_one :medications_rx_norm
has_one :medications_rxnorm_ndc, :through => :medications_rx_norm,
:source => :application, dependent: :nullify

accepts_nested_attributes_for :medications_rx_norm, :allow_destroy =>
:true

has_one :patient
has_one :patients_ptID, :through => :patient, :source => :application,
dependent: :nullify

accepts_nested_attributes_for :patient, :allow_destroy => :true

has_one :pharm_manufacturer
has_one :pharm_manufacturers_phID, :through => :pharm_manufacturer,
:source => :application, dependent: :nullify

accepts_nested_attributes_for :pharm_manufacturer, :allow_destroy =>
:true

end

Please let me know ASAP if you need anything else!!

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2564a3833600af86db7d33da82e4b0cd%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment