undefined method user_path' form_for - ruby-on-rails

I have a UsersController and it has below code
def sign_up
#user = User.new
end
And my view page has
<div class="col-md-6 col-md-offset-3">
<%= form_for #user do |f| %>
<%= f.label :first_name%>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.submit "Register", class: 'btn btn-primary'%>
<% end %>
</div>
My routes.rb file contains the following entry
get 'signup' => 'users#sign_up'
But When I submit the form, it says
ActionView::Template::Error (undefined method `users_path' for #<#<Class:0x00000004d91490>:0x00000004d90220>)
Why does this throw an error and do I need to explicity point to url in the form_for?? Why is it so??

Change your routes to:
resources :users, only: [:new, :create], path_names: {new: 'sign_up'}
and rename your sign_up action back to new. The reason you are getting the error is rails trying to guess the correct url for given resource. Since you have passed #user, which is an instance of User class, it will try to call "#{#user.class.model_name.route_key}_path key, which results in the error you got.
To solve the issue you need either make your routes to define users_path or you need to specify the url directly using url option. users_path can be defined by either index or create action, so the above solution will work (and will not create remaining CRUD routes, yey!)

Try changing this line:
<%= form_for #user do |f| %>
to this:
<%= form_for(#user, url: 'signup') do |f| %>

Related

"No route matches [POST]" despite Resources in my Routes file

I am creating a Rails app, and I need a form to function in one of my views and submit data to a table without the use of a scaffold (like I usually do).
Now, the place where this comment form is going to appear is in one view within the blog folder. It will need to allow the user to put in their comment, save it to the table, and then return to the same page.
While this is a pretty commonplace error, I am confused because I am specifying two things that seem critical: creating resources in my routes file for the form, and second, using a create method in my controller.
In the blog.html.erb, this happens in this form:
<%= form_for :cements do |f| %>
<div class="form-group">
<div class="field">
<%= f.label :post %><br>
<%= f.text_area :post, class: "form-control" %>
</div>
</div>
<h5 id="username">Username</h5>
<div class="form-group">
<div class="field">
<%= f.text_field :username, class: "form-control" %>
</div>
</div>
<%= f.hidden_field :slug, :id => "hiddenPicker"%>
<div class="actions">
<%= f.submit "Save", class: "btn btn-success-outline" %>
</div>
<% end %>
Then, in my controller, I have a create method that should redirect back to the original page, as I wanted.
blogs_controller.rb
class BlogsController < ActionController::Base
def index
#posts = Post.order('updated_at DESC').all
#comments = Cement.all
end
def blog
#posts = Post.where(slug: params[:id]).all
#comments = Cement.all
end
def create
#cements= Cement.new(story_params)
#cements.save
redirect_to(:back)
end
private
def story_params
params.require(:cements).permit(:username, :post, :slug)
end
end
Good news: the comment form renders in the view. Bad news: when I submit, I am getting this error: No route matches [POST] "/blog".
My expectation is this will be an issue with my Routes file; however, I have a resources method already in there:
Rails.application.routes.draw do
resources :posts
resources :cements
resources :blogs
The naming convention is the same as my controller file, so I am confused why this error is happening. Any ideas?
:cement is not an object it is just a symbol, so how rails will determine where to POST form? If you inspect your form you will see form action as /blog (current page url).
You should either do
<%= form_for :cements, url: cements_path do |f| %>
or
<%= form_for Cement.new do |f| %>
Both of above will generate form action as /cements, which will submit to CementsController create action, But I see in your case you want to submit it to BlogsController so use the appropriate routes(blogs_path). You can use url in second version also.

Routing a resource to controller

How do you route a resource to its controller? I am using the resource in an edit page for a different model, so my actions are being routed to its model controller first.
This edit page requests from
class Grandstreamers::ResellersController < ApplicationController
def new
end etc...
I am trying to route the requests to here instead:
Grandstreamers::CertificatesController < ApplicationController
def new
end
def update
end etc...
This is my form under views/grandstreamers/resellers/edit.html.erb
<%= form_for #untrained, :url => certificates_update_path(#untrained) do |f| %>
<p> Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(#current_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Un-Train", class: "btn btn-large btn-primary" %>
<% end %>
<%= form_for #trained, :url => certificates_create_path(#trained) do |f| %>
<p> Non-Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(#non_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Train", class: "btn btn-large btn-primary" %>
<% end %>
My route is:
resources :certificates
Note that the
:url => certificates_create_path
is not correct and not working. Is there a way to specify this route in my routes.rb file or in my form? Thank you
EDIT
This is my resellers_controller edit() which is routes to first.
#trained = Certificate.new(params[:certificate])
#Trying to get to certificates_controller update. Then update the :attend to "No"
##untrained = Certificate.new(params[:certificate])
##untrained = Certificate.find(params[:id])
##untrained = Certificate.find_by_user_id(params[:id])
#untrained is not defined, I am not sure how to get it to just go to my certificate controller. For #trained I can define it since its not made yet and does not give me errors when it cant find a correct value.
My certificates controller which uses create() but cannot get to update()
def create
#trained = Certificate.new(params[:certificate])
if #trained.save
#trained.update_attributes(attend: "Yes")
end
redirect_to grandstreamers_resellers_path
end
def update
#untrained = Certificate.find(params[:id])
#untrained.update_attributes(attend: "No")
redirect_to grandstreamers_resellers_path
end
Major Issue
The instance variable #trained and #untrained need to be defined somehow in reseller_controller. What can I define them as to load the edit page?
Part Solution
I defined this is in my resellers_controller and it loads the edit page now.
#untrained = User.find(params[:id])
Now I get this error:
No route matches [PUT] "/certificates.1"
I believe the problem is you need to let routing know the full name to the controller.
From the Rails routing guide:
scope module: 'Grandstreamers' do
resources :certificates
end
Use these paths when creating the form:
<%= form_for #untrained, :url => certificate_path(#untrained), :method => :put do |f| %>
<%= form_for #trained, :url => certificates_path, :method => :post do |f| %>
use this his routes.rb file
namespace :grandstreamers do
resources :certificates
end

Rails simple form routing issue

I have a form in Rails
<div class="page-header">
<h3>Create Blah</h3>
</div>
<%= simple_form_for #blah do |f| %>
<%= f.input :id %>
<%= f.input :name %>
<%= f.input :pho %>
<%= f.input :fun %>
<%= f.submit :class => 'btn btn-primary' %>
<% end %>
<br>
When I click the submit button, where does the code attempt to go? Does it call the create method for blah_controller.rb? Because currently, I get a routing error
Routing Error
uninitialized constant BlahsController
Here is the BlahController#create method:
def create
authorize! :create, :blahs
#blah = Blah.new(params[:blah])
if #blah.save
redirect_to admin_blah_path(#blah), :notice => 'New blah created!'
else
render :new
end
end
In my rake routes, I have
admin_blahs GET /admin/blahs(.:format) admin/blahs#index
POST /admin/blahs(.:format) admin/blahs#create
new_admin_blah GET /admin/blahs/new(.:format) admin/blahs#new
edit_admin_blah GET /admin/blahs/:id/edit(.:format) admin/blahs#edit
admin_blah GET /admin/blahs/:id(.:format) admin/blahs#show
PUT /admin/blahs/:id(.:format) admin/blahs#update
DELETE /admin/blahs/:id(.:format) admin/blahs#destroy
It looks like your BlahsController is a namespaced controller, living under the Admin module (i.e., its fully-qualified name is Admin::BlahsController). If so, when constructing forms you must also provide the :admin namespace, using something like the following:
<%= simple_form_for [:admin, #blah] do |f| %>
See the Rails Guide to Form Helpers, under the "Dealing with Namespaces" section.

NoMethodError in Shop#new

I am getting an error when I try to add a barber to my database.
I get the following error:
NoMethodError in Shop#new
Showing /Users/Augus/Rails/Barbershop/app/views/shop/new.html.erb where line #3 raised:
undefined method `barbers_path' for #<#<Class:0x105e7b818>:0x105bfe360>
Extracted source (around line #3):
1: <H1>New barber</H1>
2:
3: <%= form_for #barber do |f| %>
4: <%= f.text_field :name %> <br />
5: <%= f.submit %>
6: <% end %>
I don't know what I am doing wrong.
My shop_controller.rb:
def new
#barber = Barber.new
end
My view new.html.erb:
<H1>New barber</H1>
<%= form_for #barber do |f| %>
<%= f.text_field :name %> <br />
<%= f.submit %>
<% end %>
<%= link_to 'Back', shop_path %>
I also get this in my routes:
resources :shop
By default Rails tries to figure out the path for sending forms based on the object you pass in. In your case Rails tries to build path for sending the form based on your #barber object. As your routes.rb shows, you haven't written resources :barbers and your form fails to find proper path. You have to specify your path directly. Like this way:
<%= form_for #barber, url: shop_path do |f| %>
When you submit a new record form, the function controller#create is called. controller#new is the function that generates the page containing the form.
You need a BarberController with #create method.
Create the resource like this:
resources :shop do
resources :barber
end
A shop contains barbers.
Then form_for [#shop, #barber], :action => 'create' will trigger BarberController#create the last line of which should probably redirect to shop.
Instead of:
<%= form_for #barber do |f| %>
Try this:
<%= form_for(#barber) do |f| %>

Why is this rails form not finding the correct path?

So I've been banging my head against the wall trying to figure out why this isn't working. I keep getting
ActionView::Template::Error:
undefined method `admin_information_index_path' for #<#<Class:0x007fc67971cab8>:0x007fc67d775740>
With the trace:
# ./app/views/admin/informations/_form.html.erb:1:in `_app_views_admin_informations__form_html_erb__2815737811953353352_70245242566200'
# ./app/views/admin/informations/new.html.erb:2:in `_app_views_admin_informations_new_html_erb___3700624853666844924_70245242606040'
Any tips in the right direction?
My routes:
namespace :admin do
resources :informations
end
My controller:
class Admin::InformationsController < Admin::AdminController
def new
#information = Information.new
end
end
views/admin/informations/new.html.erb:
<h1>Add New Information Page</h1>
<%= render :partial => 'form', locals: { information: #information } %>
views/admin/informations/_form.html.erb:
<%= form_for [:admin, information] do |f| %>
<%= error_messages_for information %>
<%= f.label :title %><br>
<%= f.text_field :title %><br><br>
<%= f.label :content %><br>
<%= f.text_area :content %><br><br>
<%= f.submit "Submit" %>
<% end %>
Output of rake routes
admin_informations GET /admin/informations(.:format) admin/informations#index
POST /admin/informations(.:format) admin/informations#create
new_admin_information GET /admin/informations/new(.:format) admin/informations#new
edit_admin_information GET /admin/informations/:id/edit(.:format) admin/informations#edit
admin_information GET /admin/informations/:id(.:format) admin/informations#show
PUT /admin/informations/:id(.:format) admin/informations#update
DELETE /admin/informations/:id(.:format) admin/informations#destroy
admin_root /admin(.:format) admin/sessions#new
Try just
<%= form_for information ,:namespace=>'admin' do |f| %>
UPDATE:
Look at your routes 'informations' pluralized, but your using the singular form 'information'
You must use correct form of controller, because:
'information'.pluralize
is
"information", not informations.
So, rename controller and view folder.
I'm not sure if this will work... Just a guess.
form_for #admin.information or something along those lines.

Resources