I've got working new/create actions and a form for my #miniature model and it's nested model #scales. I can't get the update/edit actions right. Should be simple but I'm very stuck.
#miniature has_many #scales through #sizes.
In my #miniature model I have
has_many :sizes, dependent: :destroy
has_many :scales, :through => :sizes
accepts_nested_attributes_for :sizes, allow_destroy: true
In the controller I have
def new
#miniature = Miniature.new
#all_scales = Scale.all
#size = #miniature.sizes.build
end
def create
#miniature = Miniature.new(miniature_params)
params[:scales][:id].each do |scale|
if !scale.empty?
#miniature.sizes.build(:scale_id => scale)
end
end
if #miniature.save
redirect_to miniature
else
render 'new'
end
end
private
def miniature_params
params.require(:miniature).permit(:name, :release_date, :material, :pcode, :notes, sizes_attributes: [:id, :scale_id, :miniset_id])
end
That works but edit and update actions do not. I have my edit set up the same as my new.
def edit
#miniature = Miniature.find(params[:id])
#all_scales = Scale.all
#size = #miniature.sizes.build
end
I had thought that updating miniature params would update the #sizes model but it doesn't
def update
#miniature = Miniature.find(params[:id])
if #miniature.update_attributes(miniature_params)
flash[:success] = "Miniature updated"
redirect_to #miniature
else
render 'edit'
end
end
It currently updates the #miniature but not the #sizes info. Is my problem in the edit or in the update or in both?
The relevant bit of the form:
<%= f.fields_for(#size) do |sf| %>
<%= sf.label simple_pluralize(#miniature.scales.count, 'Scale') %>
<%= collection_select( :scales, :id, #all_scales, :id, :name,
{:selected => #miniature.scales.map(&:id)},
{class: 'multiselect', multiple: true}) %>
<% end %>
Any help or pointers to further reading very much appreciated. Even if it's just to say "You're overlooking this obvious thing, go and do some more reading/work".
It seems likely I need to have an update action more similar to my create action's if statement?
UPDATE for JKen13579
This is the PATCH request from my server log when submitting an edit:
Started PATCH "/miniatures/21" for 127.0.0.1 at 2014-04-02 16:00:10 +0100
Processing by MiniaturesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jQ79L1Exx83C47jnCF3nsWQ2tV07tRwKfI8wNeLzojo=", "miniature"=>{"name"=>"Test Miniature", "material"=>"Metal", "pcode"=>"123123123123123", "release_date(1i)"=>"2013", "release_date(2i)"=>"2", "release_date(3i)"=>"2", "notes"=>""}, "scales"=>{"id"=>["", "2"]}, "commit"=>"Save changes", "id"=>"21"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 4 ORDER BY "users"."id" ASC LIMIT 1
Miniature Load (0.2ms) SELECT "miniatures".* FROM "miniatures" WHERE "miniatures"."id" = ? LIMIT 1 [["id", "21"]]
(0.1ms) begin transaction
(0.2ms) commit transaction
Redirected to http://localhost:3000/miniatures/21
Completed 302 Found in 12ms (ActiveRecord: 1.3ms)
UPDATE for Observer
I'm using fields_for because that's what I managed to get the new/create action to work with. I'm not tied to it if there is a better way, at least as far as I know.
My routes has
resources :miniatures do
collection do
get :scales
get :collections
get :lines
get :contents
post :import
end
member do
get :minisets, :setminis
end
end
and further down
resources :sizes
resources :scales
I think my routes file is generally a bit cluttered and is due for a refactoring.
As per the server log entry(see below), I see that in params hash, scales (highlighted in bold) is being passed as a separate hash and not within miniature:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jQ79L1Exx83C47jnCF3nsWQ2tV07tRwKfI8wNeLzojo=",
"miniature"=>{"name"=>"Test Miniature", "material"=>"Metal",
"pcode"=>"123123123123123", "release_date(1i)"=>"2013",
"release_date(2i)"=>"2", "release_date(3i)"=>"2", "notes"=>""},
"scales"=>{"id"=>["", "2"]}, "commit"=>"Save changes", "id"=>"21"}
Change the update action as:
def update
#miniature = Miniature.find(params[:id])
if params[:scales][:id]
## Convert ["", "1","2","4","8"] to [1,2,4,8]
params[:scales][:id] = params[:scales][:id].reject(&:empty?).map(&:to_i)
## Get the scale_id from sizes already present in database [1,2,5,6]
old_scales = #miniature.sizes.pluck(:scale_id)
## Find the new scales to be added [1,2,4,8] - [1,2,5,6] = [4,8]
new_scales = params[:scales][:id] - old_scales
## Find the old_scales to be deleted [1,2,5,6] - [1,2,4,8] = [5,6]
old_scales = old_scales - params[:scales][:id]
## Build new_scales [4,8]
new_scales.each do |scale|
#miniature.sizes.build(:scale_id => scale)
end
## Delete old_scales [5,6]
Size.delete_all(:scale_id => old_scales)
end
if #miniature.update_attributes(miniature_params)
flash[:success] = "Miniature updated"
redirect_to #miniature
else
render 'edit'
end
end
Update the create action so scale_id is passed as Integer and not String:
def create
#miniature = Miniature.new(miniature_params)
if params[:scales][:id]
## Convert ["", "1","2","4","8"] to [1,2,4,8]
params[:scales][:id] = params[:scales][:id].reject(&:empty?).map(&:to_i)
params[:scales][:id].each do |scale|
#miniature.sizes.build(:scale_id => scale)
end
end
if #miniature.save
redirect_to miniature
else
render 'new'
end
end
Related
i followed a Youtube Video to implement ActiveAdmin Theme in my Rails App,- and everything worked like a charm (i thought).
https://www.youtube.com/watch?v=i2x995hm8r8
I followed every Step he took and i am a little confused right now because i can't create posts.
Whenever i try to create a New Post and type in the tile, body and select a image- it just won't do anything. It doesn't even give me a Error Message.
posts_controller.rb
class PostController < ApplicationController
def index
#post = Post.all.order('created_at DESC')
end
def create
#post = Post.new(params[:post].permit(:title, :body))
if #post.save
redirect_to #post
else
render 'new'
end
end
def show
#post = Post.find(params[:id])
#post = Post.order("created_at DESC").limit(4).offset(1)
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :body))
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
ActiveAdmin.register Post do
posts.rb
permit_params :title, :body, :image
show do |t|
attributes_table do
row :title
row :body
row :image do
post.image? ? image_tag(post.image.url, height: '100') : content_tag(:span, "No image yet")
end
end
end
form :html => {:multipart => true} do |f|
f.inputs do
f.input :title
f.input :body
f.input :image, hint: f.post.image? ? image_tag(post.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
end
f.actions
end
end
post.rb
class Post < ApplicationRecord
belongs_to :user
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true, length: { maximum: 140}
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
EDIT I:
Started POST "/admin/posts" for 127.0.0.1 at 2018-03-20 14:30:24 +0100
Processing by Admin::PostsController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"f+hfBD3lzgXEfz1q38/i3YciHsbb5LYWbbHUUsyIeOCaNSUReUUVVTBE//Dw0zXxSuFCzcMfYuUGDtIJlNb58w==", "post"=>{"title"=>"asdasdasd", "body"=>"asdasdasd"}, "commit"=>"Create Post"}
AdminUser Load (0.3ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
(0.1ms) ROLLBACK
Rendering /Users/useruser/.rvm/gems/ruby-2.4.2/bundler/gems/activeadmin-2cf85fb03ab3/app/views/active_admin/resource/new.html.arb
Rendered /Users/useruser/.rvm/gems/ruby-2.4.2/bundler/gems/activeadmin-2cf85fb03ab3/app/views/active_admin/resource/new.html.arb (134.6ms)
Completed 200 OK in 230ms (Views: 148.3ms | ActiveRecord: 5.7ms)
If you need more of my Code just tell me what i should post in here.
I am just getting started with Ruby on Rails and Programming overall, so yes indeed, i am a Newb.
Thanks in advance!
From what I see in your edit 1, I see that you render new after submiting form. It means that your Post is not being saved. It also means that your application does exactly what it should do.
I assume that you are using latests Rails 5.
In Post model, you have belongs_to association (Post belongs to User).
In Rails 5, that means user or user_id has to be provided, to create Post (Post cannot belong to noone), else you won't be able to save.
Depending how you created association in table, you might be able to pass user or user_id in params.
Another way to create post belonging to particular user is:
#user = User.first
#post = #user.posts.build(post_params)
For ActiveAdmin, you can use default form that is created based on your model.
Just make sure you permit all params when creating it that way
ActiveAdmin.register Post do
permit_params %i[title body image user_id]
...
end
You can also set belongs_to :user association as optional.
Now some general advices from me:
First of all use proper indentation.
My advice to you is install Rubocop gem.
Second:
def show
#post = Post.find(params[:id])
#post = Post.order("created_at DESC").limit(4).offset(1)
end
That doesn't make much sense, you overwrite instance variable just after first assignment.
#post = Post.order("created_at DESC").limit(4).offset(1) is more of an index action, since it does not show particular posts, it show 2..5 newest posts.
def post_params
params.require(:post).permit(:title, :body)
end
Misses image attribute.
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :body))
redirect_to #post
else
render 'edit'
end
end
You duplicate params[:post].permit(:title, :body). You hgave already created private method for that. Use it here. Same goes for creation action, you duplicated it there too. Read what DRY code is about (google it).
You have belongs_to :user but never set a user_id. A belongs_to relation is by default required (in recent rails 4/5), and this will block the saving. The easiest way, for now, to fix this is to write it as following
belongs_to :user, optional: true
[EDIT: how to store current-user as owner of post]
If you want to automatically set the user to the currently logged in user, which I think is the intention, you can add the following to your active-admin configuration:
ActiveAdmin.register Post do
# .. keep your original configuration here ...
before_build do |record|
record.user = current_user
end
end
Or add an extra field to the form allowing the administrator to select the user?
news_controller.rb
class NewsController < ApplicationController
before_action :set_news, only: [:show]
before_action :authenticate_user!, except: [:show, :index ]
def index
#news = News.where( state: true ).paginate(:page => params[:page], :per_page => 12).order('id DESC')
end
def new
#news = News.new
end
def show
#news_photos = #news.news_photos
end
def create
#news = News.new(news_params)
if #news.save
if params[:images]
params[:images].each do |image|
#news.news_photos.create(image: image)
end
end
#news_photos = #news.news_photos
redirect_to edit_news_path(#news), notice: "Saved..."
else
render :new
end
end
def destroy
#news = News.find(params[:id])
#news.destroy
respond_to do |format|
format.html { redirect_to news_index_path }
# format.json { head :no_content }
end
end
def search
#news = News.search(params[:search])
end
private
def set_news
#news = News.find(params[:id])
end
def news_params
params.require(:news).permit( :title, :description, :category, :keywords, :user_id, :email, :user_name)
end
end
news.rb
class News < ApplicationRecord
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :finders, :history]
def slug_candidates
[ :title,
[:title, :id]
]
end
def self.search(search)
pg_search_scope :search_full_text,
:against => :full_text,
:using => { :tsearch => { :prefix => true } }
end
end
index.html.erb
<div class="fh5co-box">
<h3 class="heading">Search</h3>
<%= form_tag(look_path, :method => "get") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search news" %>
<%= submit_tag "Search" %>
<% end %>
</div>
</div>
</div>
routes.rb
resources :news, :except => [ :search]
get 'news/search' => 'news#search', :as => 'look'
As I submit the data inside the search field, it is routing to the show action instead of search action.
the routes are
ews_index GET /news(.:format) news#index
POST /news(.:format) news#create
new_news GET /news/new(.:format) news#new
edit_news GET /news/:id/edit(.:format) news#edit
news GET /news/:id(.:format) news#show
PATCH /news/:id(.:format) news#update
PUT /news/:id(.:format) news#update
DELETE /news/:id(.:format) news#destroy
look GET /news/search(.:format) news#search
log:
Started GET "/news/search?utf8=%E2%9C%93&search=tax&commit=Search" for 183.83.117.57 at 2017-04-10 07:36:13 -0500
Processing by NewsController#show as HTML
Parameters: {"utf8"=>"▒~\~S", "search"=>"tax", "commit"=>"Search", "id"=>"search"}
^[[1m^[[36mNews Load (0.5ms)^[[0m ^[[1m^[[34mSELECT "news".* FROM "news" WHERE "news"."slug" = $1 ORDER BY "news"."id" ASC LIMIT $2^[[0m [["slug", "search"], ["LIMIT", 1]]
^[[1m^[[36mNews Load (0.7ms)^[[0m ^[[1m^[[34mSELECT "news".* FROM "news" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_id" = "news"."id" AND "friendly_id_slugs"."sluggable_type" = $1 WHERE ("friendly_id_slugs"."sluggable_type" = 'News' AND "friendly_id_slugs"."slug" = 'search') ORDER BY "friendly_id_slugs"."id" DESC LIMIT $2^[[0m [["sluggable_type", "News"], ["LIMIT", 1]]
Completed 404 Not Found in 5ms (ActiveRecord: 1.2ms)
ActiveRecord::RecordNotFound (can't find record with friendly id: "search"):
app/controllers/news_controller.rb:111:in `set_news'
Don't know where I have gone wrong.Is there anything to do with the slugs???
Any Help is highly Appreciated.Thanks in Advance!!!
Routes are search for in order.
So in other words you show action matches the get request for /news/(:id) and your system processes SHOW. Move the line:
get 'news/search' => 'news#search', :as => 'look'
higher up in the routes file and it should work fine, keep in mind though that this will block search from being used as a id/slug. A cleaner route would be
get '/search/news' => 'news#search', :as => 'look'
as it'd get outside the news scope and you wouldn't have conflicts with the news model.
I set up a has_many belongs_to relationship in my two models and followed Ryan Bates' screencast on how to set up the controller. When I submit my form to create the new object, the nested object does not save for some reason. Here are my models:
class Auction < ActiveRecord::Base
has_many :bids, dependent: :destroy
end
class Bid < ActiveRecord::Base
belongs_to :auction
belongs_to :user
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :auction_id, presence: true
end
and my nested object controller:
class BidsController < ApplicationController
def index
#auction = Auction.find(params[:auction_id])
#bids = #auction.bids
end
def new
#auction = Auction.find(params[:auction_id])
#bid = #auction.bids.build
end
def create
#auction = Auction.find(params[:auction_id])
#bid = #auction.bids.create(params[:bid])
#bid.save
if #bid.save
flash[:success] = "Bid has been successfully placed."
else
#bid.errors
render 'new'
end
end
def destroy
#auction = Auction.find(params[:auction_id])
#bid = #auction.bids.find
#bid.destroy
flash[:notice] = "Successfully destroyed Bid."
redirect_to auction_url(#bid.article_id)
end
end
my form:
<h1>Create a New Bid</h1>
<%= form_for ([#auction, #bid]) do |f|%>
<p>
<%= f.submit %>
</p>
<%end%>
and my terminal output:
Started POST "/auctions/1/bids" for 127.0.0.1 at 2014-11-30 17:59:13 -0600
Processing by BidsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"dkZBcab1rgZjtJGF3LAJ//exK6liglZ0Fy4mg7HWEt0=", "commit"=>"Create Bid", "auction_id"=>"1"}
Auction Load (0.1ms) SELECT "auctions".* FROM "auctions" WHERE "auctions"."id" = ? LIMIT 1 [["id", 1]]
(0.0ms) begin transaction
(0.0ms) commit transaction
(0.0ms) begin transaction
(0.0ms) rollback transaction
(0.0ms) begin transaction
(0.0ms) rollback transaction
Thanks for your help.
Your bid object needs a user_id because you have validates :user_id, presence: true in the class definition.
When you call #bid.save in the controller, however, #bid does not have a user_id value, therefore the transaction gets rolled back because of the failing validation.
You should be able to see this by looking at #bid.errors.full_messages in the create action, after you've called #bid.save. (Look up the pry gem if you're not already familiar with it...it would be a perfect tool to let you do this inspection.)
Try replacing your create action with this:
def create
#auction = Auction.find(params[:auction_id])
#bid = #auction.bids.new params[:bid].merge(user_id: current_user.id)
if #bid.save
flash[:success] = "Bid has been successfully placed."
else
flash[:error] = #bid.errors.full_messages.join('. ')
render 'new'
end
end
This assumes that you have access to the current user in the controller as current_user. Devise and other popular auth solutions supply this, or you can do so yourself.
Note also that your original code tries to write #bid to the database 3 separate times, which is twice more than you need to. Here are the offending lines:
def create
...
#bid = #auction.bids.create(params[:bid])
#bid.save
if #bid.save
...
#create instantiates an object and attempts to write it to the database. In my code above, I've replaced #auction.bids.create(params...) with #auction.bids.new(params...). This initializes #bid without trying to persist it to the db.
I also removed the first #bid.save because the line below it if #bid.save will accomplish the same thing.
Finally, your line #bid.errors doesn't do anything useful. I modified it to store the error messages in your flash hash, which you can then use in your view to display the errors to the user.
In my rails app, a Timesheet has_many Entries and an Entry belongs_to a Timesheet.
class Timesheet < ActiveRecord::Base
has_many :entries, order: 'position', dependent: :destroy
end
class Entry < ActiveRecord::Base
belongs_to :timesheet
end
I'm following Railscast 147 for sortable lists (the updated version). In the development log I notice that my params hash correctly updates the sort order, but on reload it doesn't save the positions correctly. Furthermore, the request is being processed by the create action instead of my custom sort action. Here's my controller.
class EntriesController < ApplicationController
before_filter :signed_in_user
before_filter :find_timesheet
def index
#entries = #timesheet.entries.order("position")
#entry = #timesheet.entries.build
end
def create
#entry = #timesheet.entries.build(params[:entry])
#entry.position = #timesheet.entries.count + 1
if #entry.save
#flash[:notice] = "Entry created"
#redirect_to timesheet_entries_path
respond_to do |format|
format.html { redirect_to timesheet_entries_path }
format.js
end
else
flash[:alert] = "Entry could not be added"
render 'new'
end
end
def destroy
#entry = #timesheet.entries.find(params[:id])
#entry.destroy
respond_to do |format|
format.html { redirect_to timesheet_entries_path, flash[:notice] = "Entry destroyed" }
format.js
end
end
def sort
params[:entry].each_with_index do |id, index|
#timesheet.entries.update_all({position: index+1}, {id: id})
end
render nothing: true
end
private
def find_timesheet
#timesheet = Timesheet.find(params[:timesheet_id])
end
end
and my routes.rb file.
Sledsheet::Application.routes.draw do
resources :timesheets do
resources :entries, only: [:index, :create, :destroy] do
collection { post :sort }
end
end
end
The entries.js.coffee
jQuery ->
$("#entries tbody").sortable(
helper: fixHelper
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
).disableSelection()
The output from the development log
Started POST "/timesheets/8/entries" for 127.0.0.1 at 2012-06-04 20:14:18 -0400
Processing by EntriesController#create as */*
Parameters: {"entry"=>["60", "59", "61"], "timesheet_id"=>"8"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'qDs53hgOWfRMbNN9JKau3w' LIMIT 1
Timesheet Load (0.1ms) SELECT "timesheets".* FROM "timesheets" WHERE "timesheets"."id" = ? ORDER BY date DESC LIMIT 1 [["id", "8"]]
Completed 500 Internal Server Error in 2ms
NoMethodError (undefined method `stringify_keys' for "60":String):
app/controllers/entries_controller.rb:11:in `create'
I googled the error about the undefined method, but I'm confused why the create action would be called in this case anyway? I do have a new_entry form on the page, that creates a new entry via Ajax. Perhaps this is interfering with the sort? Any help would be appreciated!
The reason why there's no 'stringify_keys' method is because you're passing an array to the create action and not the sort action.
What do you have for data-update-url in your erb.html file?
Should be sort_entries_path.
Sorry i really didn't know how to phrase the question any better but here is my problem:
when i try to update or create a patient object in my rails application the values are not getting sent through to the model, because when i try to create a new patient i get validation errors that i put in place sating i have to enter values (which i did), and when i update an existing patient object the values don't change even though i get the message of "successfully updated patient"
any ideas why that might be?
it used to work fine and i didn't change anything in the patient controller or model for it to stop working?
if you need any code from me please just let me know what u need.
controller create and update code:
class PatientsController < ApplicationController
before_filter :require_user
load_and_authorize_resource
def new
#patient = Patient.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #patient }
end
end
def edit
#patient = Patient.find(params[:id])
end
def create
#patient = Patient.new(params[:patient])
respond_to do |format|
if #patient.save
format.html { redirect_to(#patient, :notice => 'Patient was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
def update
#patient = Patient.find(params[:id])
respond_to do |format|
if #patient.update_attributes(params[:patient])
format.html { redirect_to(#patient, :notice => 'Patient was successfully updated.') }
else
format.html { render :action => "edit" }
end
end
end
end
server Log:
Parameters: {"commit"=>"Update", "authenticity_token"=>"7ypFp3DhLokjvqau06+EOOoEU2T/7UmU5OaAZuGxC1M=", "id"=>"4", "patient"=>{"occupation"=>"nothing", "blood_type"=>"O+", "next_of_kin"=>"mo man", "address"=>"arcklow", "m_name"=>"gfjhgjgfj", "date_of_first_admission(1i)"=>"2006", "tel_number"=>"45345435", "weight"=>"85.0", "date_of_first_admission(2i)"=>"3", "f_name"=>"Allan ", "date_of_first_admission(3i)"=>"3", "mobile_number"=>"43534543", "universal_ID"=>"bebo", "sex"=>"Female", "medical_history"=>"Wrist is", "height"=>"187.0", "family_history"=>"", "bmi"=>"15", "allergies"=>"", "date_of_birth(1i)"=>"1986", "date_of_birth(2i)"=>"8", "email"=>"allandx#gmail.com", "current_medication"=>"ibrufen", "date_of_birth(3i)"=>"17", "l_name"=>"Dixon"}}
WARNING: Can't mass-assign these protected attributes: occupation, blood_type, next_of_kin, address, m_name, date_of_first_admission(1i), tel_number, weight, date_of_first_admission(2i), f_name, date_of_first_admission(3i), mobile_number, universal_ID, sex, medical_history, height, family_history, bmi, allergies, date_of_birth(1i), date_of_birth(2i), email, current_medication, date_of_birth(3i), l_name
[4;35;1mPatient Load (0.1ms)[0m [0mSELECT "patients".id FROM "patients" WHERE ("patients"."email" = 'allandx#gmail.com' AND "patients".id 4) LIMIT 1[0m
[4;36;1mPatient Load (0.1ms)[0m [0;1mSELECT "patients".id FROM "patients" WHERE ("patients"."universal_ID" = 'bebo' AND "patients".id 4) LIMIT 1[0m
[4;35;1mPatient Load (0.1ms)[0m [0mSELECT "patients".id FROM "patients" WHERE ("patients"."l_name" = 'Dixon' AND "patients".date_of_birth = '1986-08-17' AND "patients".f_name = 'Allan ' AND "patients".tel_number = 45345435 AND "patients".id 4)
model code:
class Patient < ActiveRecord::Base
#patient can have many apointments and vistis
has_many :appointments, :dependent => :destroy
has_many :visits, :dependent => :destroy
# adding the photo as an attribute to patient
attr_accessible :photo
has_attached_file :photo, :styles => { :small => "200x200>" }
# validate that fields are not blank
validates_presence_of(:f_name, :l_name, :tel_number, :address)
# validate that fields are numeric
validates_numericality_of(:height, :weight, :bmi, :tel_number)
#v alidate uniqueness of fields and users
validates_uniqueness_of(:email, :universal_ID)
validates_uniqueness_of(:l_name, :case_sensitve => false, :scope => [:date_of_birth, :f_name, :tel_number], :message => "User already existsts")
#validate Email is right format
validates_format_of(:email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => "Dosen't look like a real email address, please try again")
#validate the date of birth
validates_inclusion_of(:date_of_birth, :in => Date.civil(1900, 1, 1) .. Date.today, :message => "Must be between the year 1900 and today")
end
thanks
# adding the photo as an attribute to patient
attr_accessible :photo
Here is your problem maybe you wanted attr_accesor instead of attr_accesible ?