Routing Error No route matches [POST]"/contacts/new - ruby-on-rails-5

I seem to be stuck on a Ruby on Rails tutorial. I keep getting the error that is in the title of this post, which is:
Routing Error No route matches[POST]"/contacts/new"
app/views/contacts/new.html.erb
<div class="container">
<div class="rpw">
<h3 class="text-center">Contact Us</h3>
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<div class="well">
<%= form_for "/contacts" do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-success' %>
<% end %>
</div>
</div>
</div>
routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts
end
contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to new_contact_path, notice: "Message sent."
else
redirect_to new_contact_path, notice: "Error occured"
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
contact.rb
class Contact < ActiveRecord::Base
end
I am unsure where I am going wrong. Please help me. I have been trying to fix this for a few days now.

In new.html.erb
try changing
<%= form_for "/contacts" do |f| %>
to
<%= form_for #contact do |f| %>

Related

Data does not appear in table

I have a simple form that allows you to select users and select a hospital. The form successfully submits. However when I view the index.html.erb, it looks like the following: [imgur][1]. Any idea why the "Full name" and "Hospital" is not appearing?
<div class="field">
<%= f.label :booking_reference %>
<br/>
<%= f.text_field :booking_reference %>
</div>
<div class="field">
<%= fields_for :User do |user| %>
<%= user.collection_select :user_id, User.all, :id, :full_name %>
<% end %>
</div>
<div class="field">
<%= fields_for :hospital do |hosp| %>
<%= hosp.collection_select :hospital_id, Hospital.all, :id, :name %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Wouldn't that be like
<div class="field">
<%= f.collection_select :hospital_id, Hospital.all, :id, :name %>
</div>
and
<div class="field">
<%= f.collection_select :user_id, User.all, :id, :full_name %>
</div>
?
Otherwise you break the form helper.
In doubt, check the api here: collection_select

rails carrierwave upload image or image url error

I have installed the carrierwave gem.
my model is
class Rating < ActiveRecord::Base
attr_accessible :pic_url, :rating
mount_uploader :pic_url , ImageUploader
end
and my view for is
<%= form_for #rating, :html => {:multipart=>true} do |f| %>
<div class="field">
<%= f.file_field :pic_url %>
</div>
<div class="field">
<%= f.label :remote_pic_url_url, 'or image url' %>
<br/>
<%= f.text_field :remote_pic_url_url %>
</div>
<div class="actions">
<%= f.submit 'Upload Picture', :class => 'btn btn-primary' %>
</div>
<% end %>
I get an error msg Can't mass-assign protected attributes: remote_pic_url_url
when I remove the optional pic url field , then It works.
Just add remote_file_url to attr_accessible.

Why am I getting undefined method `model_name' for NilClass:Class?

I get this when I visit a user's profile page. so localhost:3000/users/1 for example...
I get it here in line 1
1: <%= form_for(#micropost) do |f| %>
2: <%= render 'shared/error_messages', object: f.object %>
3: <div class="field no-indent">
4: <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
so then I have two micropost forms one is here
<%= form_for(#micropost, :html => { :id => "sale" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and another one is here
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
Here is my routes.
SampleApp::Application.routes.draw do
resources :users do
resources :comments
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy] do
resources :comments
resources :kind
end
resources :relationships, only: [:create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
and here is the code for the page itself (users show)
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
<div class="span8">
<% if #user.microposts.any? %>
<h3>Purchases I am interested in (<%= #user.microposts.count %>)</h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
</div>
also I dont understand how you can pass an empty object into form for? Isn't the whole point of the form to create the object?
According to the article which I stumbled upon today it may be that the object you are passing to form_for (#micropost in this case) is nil or empty.
Here's the mentioned article: http://schneems.com/post/31460949407/raise-hell-better-programming-through-error-messages

json format in ruby on rails?

How to send data in database and show in needed part by using json format in ruby on rails3?
my form is like this
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<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 :login %><br />
<%= f.text_field :login %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In the controller:
def show
#user = User.find(params[:user_id]
respond_to do |format|
format.json { :render => #user }
end
end
You can customise the JSON output by overriding the as_json in the User model:
def as_json(options=false)
self.include_root_in_json = false
options = (options || {}).reverse_merge(
:except => [:updated_at, :created_at, :id],
:include => :some_association
)
super(options)
end

Rails 3 Nested Attributes

I've had this issue for a few days and looked over every tutorial and stack overflow. I cannot seem to resolve. I can't seem to use nested attributes that work. I get an error with this form view.
**Form View**
<%= form_for #trip, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :start, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :start, :class => 'text_field' %>
</div>
<div class="control-group">
<%= f.label :end, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :end, :class => 'text_field' %>
</div>
<div class="control-group">
<% f.fields_for :driver do |d| -%>
<%= d.label :driver, :class => 'control-label' %>
<div class="controls">
<%= d.text_field :driver, :class => 'text_field' %>
</div>
Model:
class Trip < ActiveRecord::Base
attr_accessible :end, :start
belongs_to :driver
belongs_to :customer
accepts_nested_attributes_for :driver
end
class Driver < ActiveRecord::Base
attr_accessible :name
has_many :trips
end
As you said you need closing end tag.
If you have the "Can't mass-assing procteted attributes: drivers" error you must declare "attr_accessible" on :drivers_attributes at the trip model.