I am running into an error with a form I am trying to create:
ActionView::Template::Error (undefined method `contact_forms_path' for #<#<Class
The thing is I never created a contact_forms route, so I do not know why I am getting an undefined method for the contact_forms_path.
My route for the contact form is:
get "/contact_form/new", to: "contact_form#new"
My view for this form is new.html.erb within my contact_form directory
<%= simple_form_for #contact_form do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email, placeholder: 'example#email.com' %>
<%= f.input :address %>
<%= f.input :city %>
<%= f.input :state %>
<%= f.input :zip_code %>
<%= f.input :phone %>
<%= f.button :submit %>
<% end %>
My model is contact_form.rb and my controller is contact_form_controller.rb.
I could use a little direction. Any help is appreciated. I can pass along more info if needed.
Thanks!
Please try this:
add this to your routes:
post "/contact_form", to: "contact_form#create"
open up the terminal and run:
rake routes|grep contact_form
You should get something like this as a response:
contact_form_new GET /contact_form/new(.:format) contact_form#new
contact_form POST /contact_form(.:format) contact_form#create
This gives you the path for the route. Now you can specify that path in the simple_form_for:
<%= simple_form_for #contact_form, url: contact_form_path, method: :post do |f| %>
form_for assumes a default url for new Foo records as foos_path as explained here...
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
You need to specify the path that the contact_form will post to...
post "/contact_forms", to: "contact_forms#create", as: 'contact_forms"
That will be the route that receives the params when you submit the form.
Note that as a shorthand you could just specify in your routes.rb
resources :contact_forms, only: [:new, :create]
(This assumes that you will use the more conventional contact_forms#new ... convention-over-configuration is to use plural for controller names)
Related
i'm new at using simple form. In the beginning i wanted to make an easy form.
#country_controller
def edit
#country = Country.find(params[:id])
end
#edit.html.erb
<%= simple_form_for #country do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
But I'm getting this error
undefined method `country_path' for #<#<Class:0x007fdb21098e00>:0x007fdb2108cad8>
my routes
management GET /management(.:format) management/home#index
management_settings GET /management/settings(.:format) management/settings#list
management_country_index GET /management/country(.:format) management/country#index
edit_management_country GET /management/country/:id/edit(.:format) management/country#edit
management_country PATCH /management/country/:id(.:format) management/country#update
PUT /management/country/:id(.:format) management/country#update
DELETE /management/country/:id(.:format) management/country#destroy
Because your routing is slightly non-standard you would need to pass the url explicitly to simple_form_for
<%= simple_form_for #country, url: management_country_path(#country) do |f| %>
Presumably, you're using a scope:
<%= simple_form_for [:management, #country] do |f| %>
You could also use a polymorphic_path:
<%= simple_form_for #country, url: polymorphic_path([:management, #country]) do |f| %>
My problem is that when i try to call a specified method from a simple_form_for form it doesn't work.
Here is my code :
<%= simple_form_for #user, :url => {:action => :register_iban}, :html => { :method => :post } do |f| %>
<div class="col-md-8">
<%= f.input :first_name, :label => t('user-show.payment.form.first_name'), placeholder: "Prénom" %>
<%= f.input :last_name, :label => t('user-show.payment.form.last_name'), placeholder: "Nom" %>
<%= f.input :iban, :label => t('user-show.payment.form.iban'), placeholder: "IBAN" %>
<%= f.input :bic, :label => t('user-show.payment.form.bic'), placeholder: "BIC" %>
</div>
<div class="col-md-8 text-center">
<%= f.submit t('user-show.payment.title'), class: 'btn btn-danger' %>
</div>
<% end %>
So, as you can see, i try to call register_iban method from my user controller.
But when i do that, i have an error : No route matches {:action=>"register_iban", :controller=>"users", :id=>"5", :locale=>nil}
Everytime i create a new method in a controller, i have to create a route in the routes.rb file ? Here, i'd like to make this url : /users/5/register_iban (where "5" is the user id) call my method.
Sorry but i start in ruby and i'm pretty stuck :/
in your config/routes.rb try to add in the users resources
resources users do
member do
post :register_iban
end
end
No route matches {:action=>"register_iban", :controller=>"users", :id=>"5", :locale=>nil}
This error means you're trying to access a route which doesn't exist.
The routes are defined at config/routes.rb:
#config/routes.rb
resources :users do
post :register_iban
end
This will allow you to call the register_iban method in the users controller through your form.
You'll also want to make sure you're calling routes with the appropriate helpers:
<%= simple_form_for #user, url: user_register_iban(#user) %>
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| %>
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| %>
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.