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| %>
Related
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)
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.
<%= text_area_tag :body_html, "", :class =>"required mceEditor", :id=>"txaEditableContent" %>
This is my code. I want this text_area_tag to be assigned to an object(object is note in my case) just like we use "form_for object" so that the :body_html goes straight into params[:note] on submit. How can I do this?
You have got to set first an instance of Note in the controller corresponding action:
#note = Note.new
Then, in your view put this form:
<%= form_for #note do |f| %>
<%= f.text_area :body_html, :class => "required mceEditor", :id => "txaEditableContent" %>
<%= f.submit "Submit" %>
<% end %>
Now when you click the submit button you should get the right post request as you needed.
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.