I am always getting this error message:
NoMethodError in ListsController#new undefined method `fetch_value' for nil:NilClass
Extracted source (around line #8):
7 def new
8 #list = List.new
9 end
I don't get the reason for this error ^^
My routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'lists#index'
get 'home' => 'lists#index', as: 'home'
get 'new' => 'lists#new', as: 'new'
resources :lists
end
Database name:
:lists
Model name:
list
lists_controller:
class ListsController < ApplicationController
def index
#lists = List.all
end
def new
#list = List.new
end
def show
#list = List.find(params[:id])
end
def create
#list = List.new(list_params)
if(#list.save)
redirect_to #list
else
render 'new'
end
end
def edit
#list = List.find(params[:id])
end
def update
#list = List.find(params[:id])
if(#list.update(list_params))
redirect_to #list
else
render 'edit'
end
end
def destroy
#list = List.find(params[:id])
#list.destroy
redirect_to lists_path
end
private def list_params
params.require(:list).permit(:date, :class, :lesson, :subject, :teacher, :room, :info)
end
end
new.html.erb
<%= form_for :list, url: lists_path do |f| %>
<% if #list.errors.any? %>
<% #list.errors.full_messages.each do |error| %>
<div class="alert alert-danger"><%= error %></div>
<% end %>
<% end %>
<div class="alert alert-info">Please fill out all the fields below.</div>
<p>
<%= f.label :date %><br>
<%= f.text_field :date, {:class => 'form-control'} %>
</p>
<p>
<%= f.label :class %><br>
<%= f.text_area :class, {:class => 'form-control'} %>
</p>
<p>
<%= f.label :lesson %><br>
<%= f.number_field :lesson, {:class => 'form-control'} %>
</p>
<p>
<%= f.label :subject %><br>
<%= f.text_field :subject, {:class => 'form-control'} %>
</p>
<p>
<%= f.label :teacher %><br>
<%= f.text_field :teacher, {:class => 'form-control'} %>
</p>
<p>
<%= f.label :room %><br>
<%= f.text_field :room, {:class => 'form-control'} %>
</p>
<p>
<%= f.label :info %><br>
<%= f.text_area :info, {:class => 'form-control'} %>
</p>
<p>
<%= f.submit({:class => 'btn btn-info'}) %>
</p>
<% end %>
Using the BootstrapCDN and only the default gems.
Thanks for any answers :)
In your model you have an attribute named class which is a reserved keyword which is causing issues. As I can see your code:
<p>
<%= f.label :class %><br>
<%= f.text_area :class, {:class => 'form-control'} %>
</p>
So you should never use reserved keywords as attributes because you are overriding it in that case. So when overriding it, your attribute will not consist of all the properties required by the language. That is why you are getting the error.
Rails is trying to fetch the attributes from :list which should be #list
Change the form_for in new.html.erb from
<%= form_for :list, url: lists_path do |f| %>
to
<%= form_for #list, url: lists_path do |f| %>
Related
I have try many things but still failed, i want to implement a edit and update action here.
Below is my code
home controller:
class HomeController < ApplicationController
def index
#inputs = Person.all
end
def new
#input = Person.new
end
def create
#input = Person.new(input_params)
respond_to do |x|
if #input.save
x.html {redirect_to :action => 'index'}
else
x.html {render :action => 'new'}
end
end
end
def show
#input = Person.find(params[:id])
end
def edit
#input = Person.find(params[:id])
end
def update
#input = Person.find(params[:id])
respond_to do |x|
if #input.update(input_params)
x.html {redirect_to :action => 'index'}
else
x.html {render :edit}
end
end
end
private
def input_params
params.require(:inputs).permit(:name, :weight, :height, :color, :age)
end
end
edit.html.erb
<h1>Editing Data</h1>
<%= render 'form' %>
<%= link_to 'Show', home_path %> |
<%= link_to 'Back', home_index_path %>
form.html.erb:
<%= form_for :#input do |person| %>
<div class="field">
<%= person.label :name %><br>
<%= person.text_field :name %>
</div>
<div class="field">
<%= person.label :weight %><br>
<%= person.number_field :weight %>
</div>
<div class="field">
<%= person.label :height %><br>
<%= person.number_field :height %>
</div>
<div class="field">
<%= person.label :color %><br>
<%= person.text_field :color %>
</div>
<div class="field">
<%= person.label :age %><br>
<%= person.number_field :age %>
</div>
<div class="actions">
<%= person.submit %>
</div>
<% end %>
Routes are correct, i can lead me to the edit page, however, it shows,
first problem, edit with no data pops up
New updated
You should use form_for instead of form_tag,
<%= form_for #input do |x| %>
...
and then (depending on what #input actually is!)
<%= x.text_field :age %>
It looks like you are not binding your form to your method.
http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object
gives an overview, but I'd suggest something like this for your edit:
<%= form_for #input do |f| %>
<%= render 'form', f: f %>
<%= f.submit "Update" %>
<% end %>
and in your partial - which should be _form.html.erb
<div class="field">
<p><label for = "input_name">Name</label>:
<%= f.text_field :name %>
</div>
etc.. Now assuming you have set your routes correctly, it will go back to your update method. If you have not set up your routes in the way rails expects, then you should use:
<%= form_for #article, url: **your_update_path** do |f| %>
Just add the routes path that points to your update method, when you run rake routes.
I hope they are good, need to do nested forms with the profile of a user and their respective car.
each user has their own profile:
class PerfilController < ApplicationController
before_filter :authenticate_user!
def index
end
def show
#usuario = current_user
#usuario.perfil ||= Perfil.new
#perfil = #usuario.perfil
end
def update
#usuario = current_user
#perfil = #usuario.perfil
respond_to do |format|
if #usuario.perfil.update_attributes(perfil_params)
format.html {redirect_to #usuario, notice: "Datos Actualizados" }
else
format.html {render action: "show"}
end
end
end
private
def perfil_params
params.require(:perfil).permit(:nombre, :apellido, :rut)
end
end
I want the fund to ensure that each time a user posts a car, you can update your profile. Deputy nested form
<%= simple_form_for(#auto) do |f| %>
<%= f.error_notification %>
<%=f.fields_for #perfil do |perfil_builder|%>
<p>
<%= perfil_builder.label :nombre %><br/>
<%= perfil_builder.text_field :nombre %>
<%= perfil_builder.label :apellido %><br/>
<%= perfil_builder.text_field :apellido %>
<%= perfil_builder.label :rut %><br/>
<%= perfil_builder.text_field :rut %>
</p>
<%end%>
<div class="form-inputs">
<%= f.association :region %>
<%= f.association :ciudad %>
<%= f.association :marca %>
<%= f.input :modelo %>
<%= f.input :version %>
<%= f.input :año %>
<%= f.input :patente %>
<%= f.association :tipo_transmision %>
<%= f.association :combustible %>
<%= f.association :tipo_vehiculo %>
<%= f.association :carroceria %>
</div>
<div class="form-actions">
<%= f.button :submit, :id => 'submit' %>
</div>
<% end %>
The problem is when you want to update does not change the user data but if you publish the article. the error in the console is: Unpermitted parameter: perfil
Best regards friends.
You are sending the request to the auto controller, hence the Unpermitted parameter: perfil error and failure to update.
You can add this to your Perfil controller:
has_many: :autos
accept_nested_attributes_for: :autos
Then in the view switch it around like this:
<%= simple_form_for(#perfil) do |f| %>
<%= f.error_notification %>
<%= f.label :nombre %><br/>
<%= f.text_field :nombre %>
...
<%=f.fields_for :auto do |auto_builder|%>
<p>
<%= auto_builder.association :region %><br/>
...
See this page for more info: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
I'm trying to add a custom create action for my Book model, but i keep ending up with a "Couldn't find Book without an ID".
routes.rb:
Books::Application.routes.draw do
resources :books
resources :books do
collection do
post 'create_new_record', :action => :create_new_record
end
end
match 'create_new_record' => 'books#create_new_record', via: [:post]
The relevant controller action:
def create_new_record
#book = Book.new(book_params)
respond_to do |format|
if #book.save
format.html { redirect_to #book, notice: 'New book record created.' }
end
end
end
And my form (in new.html.erb). I'm looping through results that i get from goodreads.com.
<% #book_search.results.work.each do |stuff| %>
<%= form_for(#book, :url => create_new_record_books_path) do |f| %>
<div class="field">
<%= f.label :author %><br>
<%= f.text_field :author, :value => stuff.best_book.author.name %>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title, :value => stuff.best_book.title %>
</div>
<div class="field">
<%= f.label :isbn %><br>
<%= f.text_field :isbn, :value => stuff.best_book.isbn %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.text_field :image, :value => stuff.best_book.image_url %>
</div>
<div class="field">
<%= f.label :bookid %><br>
<%= f.text_field :bookid, :value => stuff.best_book.id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<hr>
<% end %>
The error i get when submitting the form is:
ActiveRecord::RecordNotFound in BooksController#create_new_record
on the callback
def set_book
#book = Book.find(params[:id])
end
I'm pretty much stumped now, my understanding is that it doesn't even reach the action, but instead looks for a book id that doesn't exist?
Thank you!
If you use before_filter so you don't pass an id to create action. Call your before filter the following way:
before_filter :set_book, except: [:index, :new, :create]
If you use model callback, params is unavailable in the model so pass the id some other way, for example via attr_accessor.
use #book = Book.where(id: params[:id]).first
I just get this error from my app but I don't know why it's happening. Also, my app goes to a non-specified route (sorry if it is a lot of code). This is my routes.rb:
Estaciones::Application.routes.draw do
devise_for :users
root :to => "user#index"
resources :user do
resources :car
end
get "user/new"
post "user/create"
get "user/:id" => "User#show"
end
Here is my user controller (I don't have problem here is only for refer):
Class UserController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #car.save
redirect_to :action => :show, :id => #user.id
else
redirect_to new_user_path
end
end
def show
#user = User.find(params[:id])
end
end
...my car controller:
class CarController < ApplicationController
def new
#car = Car.new
end
def create
#user = User.find(params[:user_id])
#car = #user.car.create(params[:car])
if #car.save
redirect_to :action => :show, :id => #user.id
else
redirect_to user_path(#user)
end
end
end
and this is part of my html.erb:
<h2>new car registration</h2>
<%= form_for([#user, #user.car.build]) do |f| %>
<p>
<%= f.label :brand %><br />
<%= f.text_field :brand %>
</p>
<p>
<%= f.label :color %><br />
<%= f.text_field :color %>
</p>
<p>
<%= f.label :model %><br />
<%= f.text_field :model %>
</p>
<p>
<%= f.label :year %><br />
<%= f.text_field :year %>
</p>
<p>
<%= f.submit "Create new car"%>
</p>
my index is only a test but it has this
<h1>WELCOME TO TANKING CONTROL ONLINE</h1>
<p>
<strong>for new users </strong>
<%= link_to 'sign up', :action => :new %>
</p>
and the form of user registration it this
<%= form_for :user, :url => { :action => :create } do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.password_field :password %>
</p>
<p>
<%= f.submit %>
</p>
<br>
<%= link_to '<<Back', :action => :index %>
<% end %>
You are missing an action 'index' in your CarController..
def index
end
Make sure you have an 'index.html.erb' in your 'cars' views.
[UPDATE] Your routes should be
resources :users do
resources :cars
end
Notice the plurality of users and cars.
I am having problems with my ruby on rails app. I have two models - 'patient' and 'address', a patient has one address, and an address belongs to a patient.
Patient.rb
class Patient < ActiveRecord::Base
has_many :charge_slips
has_one :address
validates_presence_of :last_name
validates_presence_of :first_name
validates_presence_of :middle_name
end
Address.rb
class Address < ActiveRecord::Base
belongs_to :patient
validates_associated :patient
end
Patient-controller.rb
class PatientController < ApplicationController
def index
#title = "Outpatient Services - Patient"
#today = Date.today.to_formatted_s(:long)
#patients = Patient.find(:all)
end
def new
#patient = Patient.new
#address = Address.new
end
def create
#patient = Patient.new(params[:patient])
#patient.created_on = Date.today.to_formatted_s(:long)
if #patient.save
#address = Address.new(params[:address])
#address.patient_id = #patient.id
if #address.save
redirect_to :action => 'index'
else
redirect_to :action => 'new'
end
redirect_to :action => 'index'
else
redirect_to :action => 'new'
end
end
end
new.html.rb
<%= content_tag('h3', 'Create New Patient') %>
<hr>
<% form_for #patient, :url => { :action => "create" } do |patient_form| -%>
<%= error_messages_for :patient %>
<%= patient_form.label :last_name, 'Last Name:' %> <%= patient_form.text_field :last_name, :size => 30 %><br>
<%= patient_form.label :first_name, 'First Name:' %> <%= patient_form.text_field :first_name, :size => 30 %><br>
<%= patient_form.label :middle_name, 'Middle Name:' %> <%= patient_form.text_field :middle_name, :size => 30 %><br>
<fieldset>
<legend>Patient's Permanent Address</legend>
<%= error_messages_for :address %>
<% patient_form.fields_for #address do |address_fields| -%>
<%= address_fields.label :street_name, 'Street Name:' %> <%= address_fields.text_field :street_name %><br>
<%= address_fields.label :barangay, 'Barangay:' %> <%= address_fields.text_field :barangay %><br>
<%= address_fields.label :city_municipality, 'City/Municipality:' %> <%= address_fields.text_field :city_municipality %><br>
<%= address_fields.label :country, 'Country:' %> <%= address_fields.text_field :country %><br>
<%= address_fields.label :zip_cide, 'Zip Code:' %> <%= address_fields.text_field :zip_code %><br>
<% end -%>
</fieldset>
<%= submit_tag "Add Patient" %>
<% end -%>
Everytime I add a new patient an error is thrown. Here is a part of the error:
ActiveRecord::AssociationTypeMismatch in PatientController#create
Address(#31360520) expected, got HashWithIndifferentAccess(#23815500)
RAILS_ROOT: C:/www/Outpatient Application Trace | Framework Trace | Full Trace
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_proxy.rb:263:in `raise_on_type_mismatch'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/has_one_association.rb:52:in `replace'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations.rb:1246:in `address='
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2740:in `send'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2740:in `attributes='
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2736:in `each'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2736:in `attributes='
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2434:in `initialize'
C:/www/Outpatient/app/controllers/patient_controller.rb:14:in `new'
C:/www/Outpatient/app/controllers/patient_controller.rb:14:in `create'
I am new to RoR and would like to learn the language through practice. I want to know what might be wrong with the code. Thanks!
First your Patient model needs an accepts_nested_attributes_for
class Patient < ActiveRecord::Base
has_many :charge_slips
has_one :address
validates_presence_of :last_name
validates_presence_of :first_name
validates_presence_of :middle_name
accepts_nested_attributes_for :address
end
Your controller can be simplified a great deal. There is no need to save the address separately since #patient.save will take care of that. You don't need to set
the created_on attribute manually since it will be set automagically :) Also when #patient.save fails you probably want to render :action => 'new' and not redirect_to :action => 'new'. This will redisplay the form with any validation errors (redirect_to will not.)
Also note that i renamed your controller class to PatientsController instead of PatientController. This will be more in line with Rails' RESTful conventions and will also help you simplify your view a bit. If you do this you'll need a map.resources :patients in your routes.db file, and you'll need to rename your files too.
class PatientsController < ApplicationController
def index
#title = "Outpatient Services - Patient"
#today = Date.today.to_formatted_s(:long)
#patients = Patient.find(:all)
end
def new
#patient = Patient.new
#patient.build_address
end
def create
#patient = Patient.new(params[:patient])
if #patient.save
redirect_to :action => 'index'
else
render :action => 'new'
end
end
end
Your view has a small error. It needs to be fields_for :address and not fields_for #address. Also since your controller is now RESTful your can remove the :url => { :action => "create" } part.
<%= content_tag('h3', 'Create New Patient') %>
<hr>
<% form_for #patient do |patient_form| -%>
<%= error_messages_for :patient %>
<%= patient_form.label :last_name, 'Last Name:' %> <%= patient_form.text_field :last_name, :size => 30 %><br>
<%= patient_form.label :first_name, 'First Name:' %> <%= patient_form.text_field :first_name, :size => 30 %><br>
<%= patient_form.label :middle_name, 'Middle Name:' %> <%= patient_form.text_field :middle_name, :size => 30 %><br>
<fieldset>
<legend>Patient's Permanent Address</legend>
<%= error_messages_for :address %>
<% patient_form.fields_for :address do |address_fields| -%>
<%= address_fields.label :street_name, 'Street Name:' %> <%= address_fields.text_field :street_name %><br>
<%= address_fields.label :barangay, 'Barangay:' %> <%= address_fields.text_field :barangay %><br>
<%= address_fields.label :city_municipality, 'City/Municipality:' %> <%= address_fields.text_field :city_municipality %><br>
<%= address_fields.label :country, 'Country:' %> <%= address_fields.text_field :country %><br>
<%= address_fields.label :zip_cide, 'Zip Code:' %> <%= address_fields.text_field :zip_code %><br>
<% end -%>
</fieldset>
<%= submit_tag "Add Patient" %>
<% end -%>
Hope this helps :)