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| %>
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| %>
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.
I'm getting some funkiness that is absolutely confounding me with Rails 3. I can't seem to get the routing to generate the proper path using the (mostly) standard _form style of the scaffold.
First off, I'm doing everything within an "admin" namespace. I'm finding that the form partial throws a routing error if I use admin_team_path(#team) to generate the path when creating a new Team, but then submitting the form when editing, it throws an error unless I use admin_teams_path.
admin_team_path(#team) where #team = Team.new throws this error:
No route matches {:controller=>"admin/teams", :action=>"show", :id=>#}
Meanwhile...
admin_teams_path(#team) where #team = throws this error:
The action 'edit' could not be found for TeamsController
In the latter case, it seems to be directing to the URL: http://localhost:3000/teams/1/edit - it's not recognizing the namespace properly.
Here's my full _form.html:
<%= semantic_form_for(#team, :url => admin_teams_path(#team)) do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :user_id %>
<%= f.input :league_id %>
<%= f.input :name %>
<% end %>
<%= f.buttons do %>
<%= f.commit_button :button_html =>{:class => "primary"} %>
<% end %>
<% end %>
What gives? What's the right way to create this form partial so it works for both new and edit actions?
Namespaces seem to be such a mess to work with.
Presuming you have defined your routes in a RESOURCEful manner, like so:
namespace :admin do
resources :teams
end
Then, in your _form partial you can let rails take care of the action like so:
<%= semantic_form_for(["admin", #team]) do |f| %>
.... #rest of the code
<% end %>
In my Ruby on Rails code, I have the following edit.html.erb file for tasks:
<%= render 'form' %>
I then have a _form template in the same directory with the following code:
<%= form_for #task do |f| %>
<%= fl.label :title %><br />
<% end %>
The problem is that I am getting an error when trying to navigate to the edit page. The error says "undefined task_path", so from what I can tell Rails is not properly identifying the path to my task.
The way the program is structured is that I have a List with many tasks, and each task has a list. The routes file declares the structure in this way:
resources :lists do
resources :tasks
end
How do I get the form_for to identify that I am trying to edit a task at /lists/:list_id/tasks/:task_id/edit?
Thank you for your help!
You are using Nested Resources the proper way to use that in a form is specifying the parent.
<%= form_for [#list, #task] do |f| %>
<%= f.label :title %><br />
<% end %>
I tried to save data in my Database using Rail 3 but i could not do that.an
Error occured like this:
NoMethodError in Vorlesungs#new
in my Controller:
class VorlesungsController < ApplicationController
def new
#vorlesung=Vorlesung.new
end
def create
#vorlesung=Vorlesung.create(params[:vorlesung])
if #vorlesung.save
#status_message = 'Student inserted successfully.'
else
render 'new'
end
end
end
and my View:
<%= form_for #vorlesung do |v| %>
Name : <%= v.text_field :Name %> <br>
Name de Professur : <%= v.text_field :Leiter_name %><br>
<%= v.submit 'Speicher'%>
<% end %>
when i changed Form_for into
<%= form_for #vorlesung do |v| %>
an error like this occured:
NoMethodError in Vorlesungs#new
when Form_for remains as:
<%= form_for :vorlesung do |v| %>
after clicking my submit button only the content of textboxes deleted
and no other effect.Thank you very much for your help
that is my full error message:
NoMethodError in Vorlesungs#new
Showing /home/babak/Management/app/views/vorlesungs/new.erb where line #1 raised:
undefined method `vorlesungs_path' for #<#<Class:0xb5f55cc0>:0xb5f54f00>
Extracted source (around line #1):
1: <%= form_for #vorlesung do |v| %>
2: Name : <%= v.text_field :Name %> <br>
3: Name de Professur : <%= v.text_field :Leiter_name %><br>
4: <%= v.submit 'Speicher'%>
and it is my route file:
Management::Application.routes.draw do
# get "vorlesungs/Show"
root :to => 'vorlesungs#Show'
match 'vorlesungs/new' =>'vorlesungs#new'
end
You haven't defined a create action in your routes.
Instead of
# get "vorlesungs/Show"
root :to => 'vorlesungs#Show'
match 'vorlesungs/new' =>'vorlesungs#new'
add
resources :vorlesungs
A good guide on this is to be found here.