undefined method ` ' for #<ContactsController:0x007fc3a821e300> - ruby-on-rails

I have been following the tutorial (http://guides.rubyonrails.org/getting_started.html ) to create my own web app for create a contact manager.
When I want to create a new contact, I have code in my contacts_controller
class ContactsController < ApplicationController
def show
#contact = Contact.find(params[:id])
end
def new
end
def create
#contact = Contact.new(contact_params)
#contact.save
redirect_to #contact
end
private
def contact_params
params.require(:contact).permit(:firstname, :lastname, :email, :phonenumber, :notes)
end
end
In the view I have
<h1>New Contact</h1>
<%= form_for :contact, url: contacts_path do |f| %>
<p>
<%= f.label :firstname %><br>
<%= f.text_field :firstname %>
</p>
<p>
<%= f.label :lastname %><br>
<%= f.text_field :lastname %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :phonenumber %><br>
<%= f.text_field :phonenumber %>
</p>
<p>
<%= f.label :notes %><br>
<%= f.text_area :notes %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
In routes.rb
Rails.application.routes.draw do
get 'welcome/index'
root 'welcome#index'
resources :contacts
end
However, when I submit the form, I got this error message.
I think I followed the tutorial correctly and have the new method in my class. This seems weird to me and I have no idea to start debugging this app.

Related

Action Controller: Exception | undefined method `fetch_value' for nil:NilClass

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| %>

Getting this rails error: ActionController::ParameterMissing in PostsController#new

Error: ActionController::ParameterMissing in PostsController#new
param is missing or the value is empty: post
I'm working on a 2 model rails app and when I try to go to the "new post" link to create a post this error comes up.
BUT none of my actual code is highlighted...a blank line is highlighted above my "private, def post-params" code.
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
error code highlights here
private
def post_params
params.require(:post).permit(:name, :photo_url, :description, :resume)
end
form.html.erb
<%= form_for :post, url: posts_path(#post), method: :post do |f| %>
<p><%= f.label :name %><br> <%= f.text_field :name %> </p>
<p> <%= f.label :photo_url %><br> <%= f.text_area :photo_url %> </p>
<p> <%= f.label :description %><br> <%= f.text_area :description %></p>
<p> <%= f.label :resume %><br> <%= f.text_area :resume %> </p>
<p> <%= f.submit %> </p>
<% end %>
I have all of the correct params from my schema listed, so I think it might have to do with syntax or my path?
Nothing I've changed in syntax or path naming has worked so far.

Rails ActiveModel::ForbiddenAttributesError

rarils 4.0.0
I'm trying to post a comments but I have an error:
ActiveModel::ForbiddenAttributesError in CommentsController#create
ActiveModel::ForbiddenAttributesError
def create
#comment = #article.comments.new(params[:comment]) #error point highlight this line
Parameters
{"utf8"=>"✓",
"authenticity_token"=>"zSq3KpEbucFQLa6XStEJ/I0+CpKPLFYcU/WGIdneeMg=",
"comment"=>{"name"=>"g12345",
"email"=>"g12345#12345.com",
"body"=>"hello hello"},
"commit"=>"Add",
"article_id"=>"5"}
my comments/new.html.erb
<%= form_for([#article, #article.comments.new], remote: true) do |f| %>
<%= tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit 'Add' %>
</div>
<% end %>
Rails 4 uses strong parameters by default. Do you have something like:
params.require(:some_param).permit(...)
or
params.permit(:list, :of, :allowed, :params)
in your CommentsController?
It would look something like this:
class CommentsController < ApplicationController
def create
#comment = #article.comments.new(comment_params) #error point highlight this line
end
private
def comment_params
params.require(:comment).permit(:name, :email, :body)
end
end

Point a form to a specific controller action ruby on rails

Does anyone know how to point the following comments form to the hello action in my comments controller below, any advice would be appreciated:
<h2>Add a comment:</h2>
<%= form_for([#venue, #venue.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
//////////////////my comments controller:
def hello
#venue = Venue.find(params[:venue_id])
#comment = #venue.comments.create(params[:comment].permit(:commenter, :body))
redirect_to venue_path(#venue)
end
You have to set route properly, for example:
resources :venues do
resources :comments do
collection do
post :hello
end
end
end
and set form url to this action's url:
<%= form_for(#venue.comments.build, url: hello_venue_comments_path(#venue)) do |f| %>

Form for related model not showing up

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.

Resources