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 %>
Related
I keep getting an error saying: undefined method `androids_path' for #<#:0x007ff5edcd5330>. It's saying the error is at line 1 in new.html.
The name of the model is Android and is at android.rb. Any advice on how to fix this?
In androidapps_controller.rb:
def new
#android = Android.new
end
In new.html I have:
<%= form_for(#android, validate:true) do |f| %>
<% #android.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<%= f.submit %>
<% end %>
routes.rb
Grabapp::Application.routes.draw do
root :to => 'iosapps#index'
get "static_pages/home"
get "static_pages/add"
get "static_pages/about"
devise_for :users
resources :iosapps
resources :androidapps
Add to your routes.rb:
resources :android
You're error is because you've asked form_for to do resource based routing!
<%= form_for(#android, validate:true) do |f| %>
But you didn't define the resource based routing required to make it work!
Your model and controller are not matched (Android vs AndroidApp), so need to specify the correct url in your form:
<%= form_for(#android, validate: true, url: androidapps_path) do |f| %>
<%= form_for(#android, validate:true) do |f| %> automatically sets up the correct HTTP method (normally POST or PUT) with the HTML markup for a form. It also assumes you have a url set up called /androids in the case of POST and /androids/:id in the case of PUT. So for this to work you need to tell rails to create the necessary routings. This is done by adding the following line in config/routes.rb namely resources :androids.
This is why is is better to match up your model and controller names, Rails can then automatically infer the correct controller actions based on the model name.
You need to read up a bit more on routing and how it works. Do it here: http://guides.rubyonrails.org/routing.html
I have worked with rails scaffold to generate forms and views automatically.
But now I had to use namespaced controller with model in root namspace.
So, I generated controller manually without scaffold, I'm having trouble working with form.
I suppose that the following code should generate a form for namespaced controller
<%= form_for #menu do |f| %>
<div class="field">
<%= f.label :label %><br />
<%= f.text_field :label %>
</div>
<div class="field">
<%= f.label :order %><br />
<%= f.text_field :order %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
but it doesnt. it says
undefined method `model_name' for NilClass:Class
it seems very easy to do rails with scaffold generators, but if one wants to do manually, rails helper method turns negative on them.
I have searched for form_for documentation and changed the code to
<%= form_for #menu, :url => admin_menu_path do |f| %>
then it shows a different error that
No route matches {:action=>"show", :controller=>"admin/menus"}
my rake routes gives the following output
admin_root /admin(.:format) admin/menus#index
admin_menus GET /admin/menus(.:format) admin/menus#index
POST /admin/menus(.:format) admin/menus#create
new_admin_menu GET /admin/menus/new(.:format) admin/menus#new
edit_admin_menu GET /admin/menus/:id/edit(.:format) admin/menus#edit
admin_menu GET /admin/menus/:id(.:format) admin/menus#show
PUT /admin/menus/:id(.:format) admin/menus#update
DELETE /admin/menus/:id(.:format) admin/menus#destroy
correct me if I'm wrong, but I think ruby cannot find model Menu in Admin namespace, which is obvious. So, I tried with ::Menu.new , I thought it would look up in upper namespace, but no result!
You might be forgetting to instantiate #menu in your controller.
The message "undefined method 'model_name' for NilClass:Class" says that #menu is nil.
Since admin_menu_path needs a Menu instance, Rails cannot generate the route correctly when nil is passed.
Once you have the instance variable properly set in your controller you can use:
form_for [:admin, #menu] do |f| ... end
Something like form_for [:admin, #menu] do |f| ... end should work. Similar question here: Nested resources in namespace form_for
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.
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 %>