undefined method contact_path in rails form - ruby-on-rails

I have my routes defined as below
get 'contacts/new', to: 'contacts#new'
And in my ContactsController I have defined as below
def new
#contact = Contact.new
end
In views contacts/new/_form.html.erb I have structured form as below
<%= form_for #contact, html: {multipart:true} do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<% end %>
But when i go to localhost:3000/contacts/new
I get the below error.
undefined method contacts_path which is occuring in first line of form.
But when i try to define as below in routes, it worked
get 'contacts/new', to: 'contacts#new', as: 'contact'
Any idea why rails throws such error when i have defined it in the routes file. I am just trying to understand inner workings of rails routes

To avoid this kind of errors, remove your route and use:
resources :contacts, only: [:new, :create]

Try better use railsy way like resources as #Graham mentioned.
or
get 'contacts', to: 'contacts#index', as: :contacts #genetares: contacts_path
get 'contacts/new', to: 'contacts#new', as: :new_contact #new_contact_path

Make a post type route for contacts (for this it is throwing error)
Or remove this route
get 'contacts/new', to: 'contacts#new'
And add this simply
resources :contacts

Related

Why can't I route the patch method of one controller to the show method of another rails 5

I'm receiving this error message:
No route matches [POST] "/users/9"
I'm trying to figure out a way to have the update method of one controller and use it in the view of a show method of another controller. This is what my route file looks like.
routes.rb
Rails.application.routes.draw do
root 'dashboards#index'
devise_for :users
resources :users, only: [:show]
patch '/users/:id' => 'companyinfos#update'
put '/users/:id' => 'companyinfos#update'
resources :dashboards, only: [:index]
end
I'm finding the correct companyinfo through the users controller
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#companyinfo = Companyinfo.find(current_user.id)
end
end
and I'm saying to use this method inside of my company_info controller
companyinfos_controller.rb
class CompanyinfosController < ApplicationController
def update
#companyinfo = Companyinfo.find(current_user.id)
if #companyinfo.update(companyinfo_params)
flash[:notice] = "Saved ..."
else
flash[:alert] = "cannot save"
end
render 'edit'
end
private
def companyinfo_params
params.require(:companyinfo).permit(:CompanyStage)
end
end
and finally I'm using form_for to update it on users_path
show.html.erb
<%= form_for :companyinfo do |f| %>
<%= f.text_field :CompanyStage %>
<%= f.submit "save", class: "btn btn-success" %>
<% end %>
Updating the information in the database works fine when I update it through companyinfo/:id/edit but if I try to update it on another view such as users/:id I'm getting the error message above. What is the proper way to go about this?
UPDATE
Actually if I just do what the error message is telling me it works. Miracle right? Another question. Is there another way to have multiple routes from different controllers pointing to the same location? Or is this the best way?
Error says: No route matches [POST]
Update routes:
post '/users/:id' => 'companyinfos#update'
Or form method:
<%= form_for :companyinfo, method: :patch do |f| %>

Rails custom action not working due to show, how to fix?

I'm trying to create a customer action in my campaigns controller called building.
http://localhost:3000/campaigns/building
Shows error:
Showing .../app/views/campaigns/show.html.erb where line #1 raised:
undefined method `name' for nil:NilClass
<h2><%= #campaign.name %></h2>
<p>
Created by: <%= #campaign.user.email %> <%= time_ago_in_words(#campaign.created_at) %> ago.
</p>
<h5>Website: <%= #campaign.website %></h5>
My routes file:
resources :campaigns do
resources :targets
end
get "campaigns/building" => "campaigns#building", :as => :campaigns_building
Controller:
class CampaignsController < ApplicationController
before_action :find_campaign, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def show
end
def building
end
...
My show.html.erb:
<h2><%= #campaign.name %></h2>
<p>
Created by: <%= #campaign.user.email %> <%= time_ago_in_words(#campaign.created_at) %> ago.
</p>
<h5>Website: <%= #campaign.website %></h5>
How do I make it not give this error?
It seems you are trying to make a custom route and unable to make it work. If so, to make it work, you should move your custom route above resources to avoid conflicts with the default resourceful route and correctly route to building action
get "campaigns/building" => "campaigns#building", :as => :campaigns_building
resources :campaigns do
resources :targets
end
This is because Rails tries to match routes starting from the top down. You can also define it as a collection route on the resources
resources :campaigns do
get "building", on: :collection
resources :targets
end

No route matches [POST] "/buddies" Following Users

(Rails 3.2 newbie, App is twitter)
I wish I understood routes, but I still don't. I've setup a system where users can follow other users by submitting their username manually:
#relationship = User.find_by_username(params[:username])
if #relationship
if current_user.following? #relationship
current_user.unfollow #relationship
else
current_user.follow #relationship
end
else
flash[:notice] = "User with username #{params[:username]} is not found"
end
Which spits out this:
undefined method `model_name' for NilClass:Class
That didn't really work until I changed #relationship to :relationship in view like this:
<%= form_for :relationship do |f| %>
<%= f.text_field :username, placheholder: "username" %>
</div>
<br/>
<%= f.submit "Add/Subtract" %>
<% end %>
Now that error message ("User with username..") pops up all-the-time BUT when I click "Add/Subtract" it changes to this:
No route matches [POST] "/buddies"
All this is under the User model.
I imagine something is terribly wrong.
EDIT: Routes looks like this.. (ribbit = tweets)
Ribbitapp::Application.routes.draw do
resources :ribbits
resources :relationships
resources :sessions
resources :users
get 'logout', to: 'sessions#destroy', as: 'logout'
#get 'buddies', to: 'users#buddies', as: 'buddies'
match 'buddies', to: 'users#buddies', as: 'buddies'
root to: 'users#new'
match 'buddies', to: 'users#buddies', as: 'buddies' it should be like this in your routes

Rails route not finding corresponding action

Inside app/views/participants/index.html.erb:
<%= form_tag bulk_add_participants_program_path do %>
<%= wrap_control_group do %>
<%= text_area_tag :bulk_add_participants, :size => "60x3" %>
<% end %>
<%= submit_tag "Import Participants and Users" %>
<% end %>
But notice that the controller and route pertain to the Program model (for good UI reasons). And I think that might be related to the problem. When I render that view I get this error message:
No route matches {:action=>"bulk_add_participants", :controller=>"programs"}
Which is weird because in app/controllers/programs_controller.rb:
def bulk_add_participants
puts "yay!" # because i am troubleshooting
end
And my config/Routes.rb is:
RepSurv::Application.routes.draw do
root to: 'programs#index'
devise_for :users, path_prefix: 'devise'
resources :users
resources :programs do
resources :participants do
resources :rounds do
get 'survey' => 'rounds#present_survey'
put 'survey' => 'rounds#store_survey'
end
end
resources :questions
resources :rounds
member do
get 'report' => 'reports#report'
get 'bulk_add_participants'
end
end
end
It's not finding the route because you have programs defined as a plural resource:
resources :programs do
When you do that and reference a member route like your bulk_add_participants, it expects a :program_id parameter in your case. (Try running rake routes, and you'll see a path like /programs/:program_id/bulk_add_participants.)
So your form_tag call should perhaps look like this:
<%= form_tag bulk_add_participants_program_path(#program) do %>

Rails 3 : Can't get form_for to work as a 'delete' following the RESTful achitecture => always giving a ROUTING ERROR

I have a very simple render that goes as follow:
<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => #user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
When I submit this form it will always give me a
Routing Error
No route matches "/relationships"
on my page.
In my relationships controller, I have created all the propers methods:
def create
...
end
def destroy
...
end
def update
...
end
def show
...
end
And in my routes config I have made sure to allow all routes for the relationships controller
resources :relationships
But I can't seem to get into the destroy method of the controller :(
However if I remove the
:html => {:method => 'delete'}
method parameter in the form_for then I get to the create method of the controller no pb.
I don't get it....
Alex
ps: this is the rake routes results for relationships:
relationships GET /relationships(.:format) {:action=>"index", :controller=>"relationships"}
POST /relationships(.:format) {:action=>"create", :controller=>"relationships"}
You should point the delete request to single resource url eg. relationships/4325. Run rake routes to view what url/verb combinations are valid.
--edit
Routes for relationship resources:
resources :relationships, :only => [:index, :create, :destroy]
Unfollow button (creates a form for itself):
= button_to "Unfollow", relationship_path(relationship), :method => 'delete'

Resources