undefined method `model_name' for NilClass:Class in edit form - ruby-on-rails-3

I have a rails app where I can create and edit records. I've created a form to enter data which works fine when I use the new/create actions. It will create a record no problem. But when I hit the edit action it gives me an undefined method 'model_name' for NilClass:Class.
I'm not sure what this means. Can someone give me a hand?
Form:
<%= form_for(#patient) do |f| %>
<%= f.label :Patient_Last_Name %>
<%= f.text_field :patient_last %>
<%= f.label :Patient_First_Name %>
<%= f.text_field :patient_first %>
<%= f.label :Patient_DOB %>
<%= f.date_select :patient_dob %>
<%= f.label :Primary_Diagnosis %>
<%= f.collection_select(:diagnosis_id, Diagnosis.all, :id, :diagnosis_name)%></br>
<%= f.label :Primary_Physician %>
<%= f.collection_select(:physician_id, Physician.all, :id, :physician_name)%></br>
<%= f.button :submit %>
<% end %>
View Code:
<td><%= link_to 'Edit', edit_patient_path(patient), :class => 'btn btn-close btn-mini'%></td>
Controller Code:
def edit
#patient = Patient.find(params[:id])
end
Edit view:
<%= render 'form' %>
When I remove the partial render from the form, the URL will go to the correct route/url. But I keep getting that error when the form partial is rendered.

There was an extra end statement in my controller which was cutting off half of the class which included the edit action. This was not allowing me to use the edit action. Once this typo was fixed things started working normally.
Sorry for the confusion.

Related

Rails 3 - Ransack - check_box_tag

Listing Model - belongs_to :area
Area Model - has_many :listings
I'm trying to implement a search using Ransack with check boxes; where user checks selected areas, search returns all the listings of the areas selected.
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq][]', area.id) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>
Console output:
Parameters: {"utf8"=>"✓", "q"=>{"area_id_eq"=>["1", "2"]}, "commit"=>"SEARCH"}
Completed 500 Internal Server Error in 4ms
NoMethodError - undefined method `to_i' for ["1", "2"]:Array:
Just not sure how to implement it to accept multiple check box values.
Instead of using "area_id_eq", use "area_id_any". You'll also want to check to make sure that your parameters are selected:
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq_any][]', area.id, (params[:q][area_id_eq_any].include? area.id.to_s) ? true : false ) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>

Weird undefined method 'all' and collection_select error

I've got a form like this (simplified, but you get the idea):
<%= form_for(#brand, :html => { :class => "form-horizontal" }) do |f| %>
<%= f.fields_for :prices do |price| %>
<%= price.collection_select(:price, :template_id, Template.all, :id, :name) %>
<% end %>
<%= f.submit "Save", :class => 'btn btn-primary' %>
<% end %>
Which when rendered gives me this error
undefined method `all' for ActionView::Template:Class
on the collection_select line.
Template.all works from the controller and the console. If I write a #templates = Template.all and use #templates in the collection_select line then I get this error:
undefined method `merge' for :name:Symbol
Any thoughts?
You can do it by prefixing with two colon. e.g,
<%= price.collection_select(:price, :template_id, ::Template.all, :id, :name) %>
but I believe, you should avoid using Template as model name as it is rails Action View Template
Solved it. It was annoyingly simple.
<%= price.collection_select(:template_id, #templates, :id, :name) %>
Duplication. Eugh.

Update form in rails - No route matches [PUT]

I have a form to create adverts.
Controllers:
def edit
#engines = Engine.all
#car = Car.find(params[:id])
end
def update
#car = Car.find(params[:id])
if #car.save
redirect_to root_path
end
end
My routes:
resources :adverts
Create.html.erb
<%= form_for #car, :url => adverts_path do |f| %>
<div><%= f.label :name %><br />
<%= f.text_field :name %></div>
<%= hidden_field_tag :model_id, params[:model_id] %>
<%= select_tag :engine_id, options_from_collection_for_select(#engines, "id", "name",:selected=>#car.engine_id) %>
<div><%= f.submit "Create car!" %></div>
<% end %>
I can create advert, but I can't to update it.
edit.html.erb
<%= form_for #car, :url => adverts_path do |f| %>
<div><%= f.label :name %><br />
<%= f.text_field :name %></div>
<%= hidden_field_tag :model_id, params[:model_id] %>
<%= select_tag :engine_id, options_from_collection_for_select(#engines, "id", "name",:selected=>#car.engine_id) %>
<div><%= f.submit "Update car!" %></div>
<% end %>
when I submited my form, I have an error - No route matches [PUT] "/adverts"
$ rake routes:
adverts GET /adverts(.:format) adverts#index
POST /adverts(.:format) adverts#create
new_advert GET /adverts/new(.:format) adverts#new
edit_advert GET /adverts/:id/edit(.:format) adverts#edit
advert GET /adverts/:id(.:format) adverts#show
PUT /adverts/:id(.:format) adverts#update
DELETE /adverts/:id(.:format) adverts#destroy
I need help.
When you are updating you have to let Rails know which object you want to update by passing an id.
In edit.html.erb change:
<%= form_for #car, :url => adverts_path do |f| %>
to:
<%= form_for #car, :url => advert_path(#car) do |f| %>
By the way, I find your code very strange. Why don't your model names match your controllers and routes? I mean you are creating an advert but your model is called car. That doesn't make any sense. Either call it car or advert, but don't mix them.
If you used RESTful routing, you don't need to specify a url, just need:
<%= form_for #car do |f| %>
The form can know #car is new record, or saved record, so it will send appropriate http method.
And in your update action:
def update
#car = Car.find(params[:id])
if #car.update_attributes(params[:car])
redirect_to root_path
end
end
I got myself in a similar situation today with a mismatched resource and model name. I agree the model and controller names need to correlate, but you can override the routes name to be whatever you want.
resources :cars, path: "adverts"
Along with RESTful routing
<%= form_for #car do |f| %>
You may also want to make sure your url: path is singular on the #form_form.

Checkbox symbols wrong number of arguments

I am having a problem with the tutorial of codelearn
See here
I have a form
<%= form_for :complete, :url => "/todos/complete", :method => :post do |f| %>
<% #todo_items.each do |t| %>
<%= f.check_box :todo_ids[], t.id %>
<%= t.todo_item %>
<% end %>
<%= f.submit "Complete todos", :class => "btn btn-success" %>
<% end %>`
I've got a problem with the symbol todo_ids[]. I get the error "wrong number of arguments (0 for 1..2)" at the line where it is written.
I tried another way with form_tag but that does not change a thing, I still get the error.
What I don't understand is that they don't have this problem in the tutorial.
Please do you have any idea ?
Many thanks

I have a two form with a single submit button. Then how to i need to save the values of those form values?

form with the single submit. How to store that values in the data base
<%= form_for (#movie) do |f| %>
//Movie
<%= f.label :moviename,"Movie Name:"%>
<%= f.text_field :moviename%>
//Releases
<%= f.fields_for :release do |release_fields| %>
<%= release_fields.label :theatre,"Theatre :"%>
<%= release_fields.text_field:theatre%>
<%= release_fields.label :city,"City :"%>
<%= release_fields.text_field:city%>
<%= release_fields.label :releasedate,"Release Date :"%>
<%= release_fields.text_field:releasedate%>
//Submit:
<%= f.submit "Save The Movie"%>
<% end %>
<% end %>
this is the form with the single submit button.There an error it showing the unknown attribute release can any on help with is issue Please
fields_for is expecting to access #movie.release, does that exist?