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.
Related
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| %>
I'm new to Rails and struggling to get my belongs_to association right. I have an app where a painting belongs to an artist and an artist can have_many paintings. I can create and edit my paintings, however I can not edit or create artists except through the console. Through much Googling I feel I have got myself turned around. Any help would be much appreciated!
Here's my routes.rb file:
MuseumApp::Application.routes.draw do
resources :paintings
resources :paintings do
resources :artists
resources :museums
end
root 'paintings#index'
end
Here's my paintings Controller
def show
#painting = Painting.find params[:id]
end
def new
#painting = Painting.new
##artist = Artist.new
end
def create
safe_painting_params = params.require(:painting).permit(:title, :image)
#painting = Painting.new safe_painting_params
if #painting.save
redirect_to #painting
else
render :new
end
end
def destroy
#painting = Painting.find(params[:id])
#painting.destroy
redirect_to action: :index
end
def edit
#painting = Painting.find(params[:id])
end
def update
#painting = Painting.find(params[:id])
if #painting.update_attributes(params[:painting].permit(:title, :image)) #safe_params
redirect_to #painting
else
render :edit
end
end
Here's the form in my paintings view:
<%= form_for(#painting) do |f| %>
<fieldset>
<legend>painting</legend>
<div>
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div>
<%= f.label :image %>
<%= f.text_field :image %>
</div>
<%= form_for([#painting,#painting.create_artist]) do |f| %>
<div>
<%= f.label :Artist %>
<%= f.text_field :name %>
</div>
</fieldset>
<%= f.submit %>
<% end %>
<% end %>
Artists Controller:
class ArtistsController < ApplicationController
def index
#artists = Artist.all
#artists = params[:q] ? Artist.search_for(params[:q]) : Artist.all
end
def show
#artist = Artist.find params[:id]
end
def new
#artist = Artist.new
end
def create
#painting = Painting.find(params[:painting_id])
#artist = #painting.create_artist(artist_params)
redirect_to painting_path(#painting)
end
def destroy
#artist = Artist.find(params[:id])
#Artist.destroy
redirect_to action: :index
end
def edit
#artist = Artist.find(params[:id])
end
def update
#painting = Painting.find(params[:painting_id])
#artist = #artist.update_attributes(artist_params)
redirect_to painting_path(#painting)
end
end
private
def artist_params
params.require(:artist).permit(:name)
end
Index view:
<h1> Hello and Welcome to Museum App</h1>
<h3><%= link_to "+ Add To Your Collection", new_painting_artist_path %></h3>
<%= form_tag '/', method: :get do %>
<%= search_field_tag :q, params[:q] %>
<%= submit_tag "Search" %>
<% end %>
<br>
<div id="paintings">
<ul>
<% #paintings.each do |painting| %>
<li><%= link_to painting.title, {action: :show, id:painting.id} %> by <%= painting.artist_name %></li>
<div id = "img">
<br><%= link_to (image_tag painting.image), painting.image %><br>
</div>
<%= link_to "Edit", edit_painting_path(id: painting.id) %>
||
<%= link_to 'Destroy', {action: :destroy, id: painting.id},method: :delete, data: {confirm: 'Are you sure?'} %>
<% end %>
</ul>
</div>
In your case you should use accepts_nested_attributes_for and fields_for to achieve this.
Artist
has_many :paintings, :dependent => :destroy
accepts_nested_attributes_for :paintings
Painting
belongs_to :artist
And also you should try creating artist with paintings like this
form_for(#artist) do |f| %>
<fieldset>
<legend>Artist</legend>
<%= f.label :Artist %>
<%= f.text_field :name %>
<%= fields_for :paintings, #artist.paintings do |artist_paintings| %>
<%= artist_paintings.label :title %>
<%= artist_paintings.text_field :title %>
<%= artist_paintings.label :image %>
<%= artsist_paintings.text_field :image %>
</fieldset>
<%= f.submit %>
<% end %>
Note:
You should be having your Artist Controller with at least new,create,edit and update methods defined in it to achieve this.
Edit
Try the reverse
Artist
has_many :paintings, :dependent => :destroy
Painting
belongs_to :artist
accepts_nested_attributes_for :paintings
form_for(#painting) do |f| %>
<fieldset>
<legend>Painting</legend>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :image %>
<%= f.text_field :image %>
<%= fields_for :artists, #painting.artists do |ff| %>
<%= ff.label :Artist %>
<%= ff.text_field :name %>
</fieldset>
<%= f.submit %>
<% end %>
Put this form in paintings views.
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 am trying to have a form show up for a related model, but it is not displaying when I view the page in a browser. How do I url field in my fields_for to display?
Here is my code:
User Model:
class UsersController < ApplicationController
def new
#user = User.new
#user.websites.build
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
end
Website Model:
class Website < ActiveRecord::Base
belongs_to :user
end
Users View:
<h1>Sign Up</h1>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
...
<% end %>
<p>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br/>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br/>
<%= f.password_field :password_confirmation %>
</p>
<% f.fields_for :websites do |builder| %>
<%= builder.label :url %><br/>
<%= builder.text_field :url %>
<% end %>
<p class="button"><%= f.submit %></p>
<% end %>
Final output:
You missed the equals sign in your erb tag.
<% f.fields_for :websites do |builder| %>
.. should be ..
<%= f.fields_for :websites do |builder| %>
Does that fix it?
It looks like you're maybe confusing singular and plural in your fields_for as well. Calling it with the plural websites then treating the block as a singular website doesn't make sense.
In my contactlist CRUD application, all the contacts are shown properly when i type the following URL: localhost/contact
However, I want to have two options for each contact- either to update the information or to delete the record. So once i click on contact,say- "Chetan", i want to go to a 'showmain' page with two options- Update(linking me to an update page) and Delete(linking me to a del page).
My controller is as follows:-
class ContactController < ApplicationController
def index
#contacts=Contact.find(:all)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
end
def show
#contact=Contact.find(params[:id])
end
def del
#contact=Contact.find(params[:id])
end
def new
#contact=Contact.new
end
def create
#contact=Contact.new(params[:contact])
if #contact.save!
redirect_to :action => "index"
else
render :action => "new"
end
end
def showmain
respond_to do |format|
format.html # index.html.erb
format.json
# #contact=Contact.new(params[:contact])
end
def update
#contact=Contact.find(params[:id])
#contact.attributes= params[:contact]
#contact.save!
redirect_to :action => "index"
end
def delete
#contact=Contact.find(params[:id])
#contact.destroy
redirect_to :action => "index"
end
end
end
And my showmain.html.erb page where i am getting the syntax error(showmain.html.erb:14: syntax error, unexpected keyword_ensure, expecting $end) is as follows:-
<h1>Do you want to Update or Delete the Attributes? </h1>
<ul>
<li>
<%= link_to "Update" ,:action=>'show',:id => #contact -%>
</li>
<li>
<%= link_to "Delete" ,:action=>'del',:id => #contact -%>
</li>
</ul>
<% end %>
<p>
<%= link_to "Back", {:action => 'index'} -%>
</p>
Need some help to remove the error..
My show.html.erb is as follows:-
<h1>View/Edit Contact</h1>
<% link_to "Update" , contact_path(#contact) do |f| -%
<%= f.label :first_name %>:
<%= f.text_field :first_name %><br />
<%= f.label :last_name %>:
<%= f.text_field :last_name %><br />
<%= f.label :address %>:
<%= f.text_field :address %><br />
<%= f.label :city %>:
<%= f.text_field :city %><br />
<%= f.label :state %>:
<%= f.text_field :state %><br />
<%= f.label :country %>:
<%= f.text_field :country %><br />
<%= f.label :phone %>:
<%= f.text_field :phone %><br />
<%= f.label :email %>:
<%= f.text_field :email %><br />
<%= f.submit "Update" %>
<% end %>
<p>
<%= link_to 'Back', {:action => 'index'} %>
</p>
def showmain
respond_to do |format|
format.html # index.html.erb
format.json
# #contact=Contact.new(params[:contact])
end #Close respond_to
end # Close the showmain