So, I'm trying some steps into rails and searched around SO, but couldn't find any answer. I constantly get NoMethodError undefined method for Line 1 as soon as I browse to /tswhois/new. Whats wrong?
controllers/tswhois_controller.rb
class TswhoisController < ApplicationController
def index
#tswhois = Tswhois.all
end
def new
#tswhois = Tswhois.new
end
end
config/routes.rb
Rails.application.routes.draw do
resources :tswhois
end
views/tswhois/new.html.erb
<% form_for (#tswhois) do |f| %>
<p>
<%= f.label :url %>
<%= f.text_field :url %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
console
rails console
Loading development environment (Rails 4.1.8)
2.1.5 :001 > Tswhois.count
(10.6ms) SELECT COUNT(*) FROM `tswhois`
=> 0
Just remember conventions in rails: model name is singular ie no s or es at the end. Routing is built around your model name. Your error page suggests Rails doesn't get the correct route for the action, in this example new action.
Can you copy and paste the entire error message ? try also: <%= form_for(#tswhois) do |f| %> without space and with the = sign
Did you restart the server after adding the resources to the routes.rb? the code you provided for your routes file is missing 'end' at the end of it.
Look into your views/tswhois/new.html.erb code. The code snippet you posted is probably from views/tswhois/_form.html.erb
It probably has a line <%= link_to 'something', tswhois_index_path %>
Change it to
<%= link_to 'something', tswhois_path %>
Thing is, the name 'tswhois' is going to be rather confusing due to the convention of singular and plural for a single instance and array of instances respectively.
Related
I'm having an issue very similar to the one asked in this question here: NoMethodError / undefined method `foobar_path' when using form_for However the answer there confuses me.
I went through Michael Hartel's Ruby on Rails tutorial before developing the application I'm working on at the moment, I tried to copy exactly what he did when he created a user model as I created my model. My application is designed to be a database for university professors, so the model I'm using is called "professor" but it's the same concept as "user".
Here is the code for my New.html.erb where is where users go to create a new professor:
<%provide(:title, 'Add a professor') %>
<div class="jumbotron">
<h2> New Professor</h2>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for (#professor) do |f| %>
<%= f.label "First Name" %>
<%= f.text_field :fname %>
<%= f.label "Last Name" %>
<%= f.text_field :lname %>
<%= f.label "School" %>
<%= f.text_field :school %>
<%= f.submit "Add this professor", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
And then here is the code from the Professor_controller.rb
class ProfessorController < ApplicationController
def show
#professor = Professor.find(params[:id])
end
def new
#professor = Professor.new
end
end
When I replace
<%= form_for (#professor) do |f| %>
In new.html.erb with:
<%= form_for (:professor) do |f| %>
It works. The thread I mentioned above said something about adding a route for the controller. My routes.rb looks like this:
Rails.application.routes.draw do
root 'static_pages#home'
get 'about' => 'static_pages#about'
get 'newprof' => 'professor#new'
resources :professor
And I don't believe that in Michael Hartel's book he does anything differently. I'm still very new to Rails so forgive me if this is a bit of an easy question, I've been stuck on it for a few days and I've tried numerous work arounds, using the instance of :professor works but #professor does not and I don't know why.
Within the Rails environment it's very important to be aware of the pluralization requirements of various names. Be sure to declare your resources as plural:
resources :professors
Declaring it in the singular may mess up the automatically generated routes, you'll get thing like professor_path instead of professors_path. You can check what these are with:
rake routes
If you get errors about x_path being missing, check that there's a route with the name x in your routes listing. The most common case is it's mislabeled, a typo, or you've failed to pluralize it properly.
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'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 %>
I'm a newbie Rails developer who is getting the following error when trying to access the 'new' action on my CityController:
undefined method `cities_path' for #<#<Class:0x104608c18>:0x104606f08>
Extracted source (around line #2):
1: <h1>New City</h1>
2: <%= form_for(#city) do |f| %>
3: <%= f.error_messages %>
4:
5: <div class="field">
As some background, I have a State model with many Cities. I'm getting this error after clicking on the following link coming from a State show page:
<p>Add a city: <%= link_to "Add city", new_state_city_path(#state) %></p>
When I run 'rake:routes' it says this is a legit route...
For more background, here is the CityController 'new' action:
def new
#city = City.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #city }
end
end
Here is the (complete) form in the view:
<%= form_for(#city) do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This initially made me think that it's a resources/routes issue since it came back with a mention of 'cities_path' (in fact, that's what another person posting to Stack Overflow had wrong (Rails error "NoMethodError" - My first ruby app). However, that doesn't seem to be the case from what I can see. Here are how my resources look in my routes file:
resources :states do
resources :cities
end
I can get it working when they are not sub-resources, but I really need to keep them as sub-resources for my future plans with the app. Any help would be very much appreciated, since I've been racking my brains on this for more hours than I would care to admit... Thanks!
(Not sure this matters at all, but I'm running the very latest version of Rails 3 beta2).
Your problem is coming from line 2 of your view above, specifically the form_for declaration. As you pointed out, state_city_path is a valid path, but right now, your form is not using this path, it's using city_path. When using nested resources, you need to define everything in terms of that nesting. Your form_for should look something like form_for([#state, #city]) do (I don't remember the exact syntax).
Your follow up answer will work, but isn't exactly the best way to go about it, unless you want to be able to look at cities that are not in the context of a state.
Hope this helps.
PS. The form_for documentation is pretty good, and shows some good examples when using it with resources.
The problem is most likely in this line:
<p>Add a city: <%= link_to "Add city", new_state_city_path(#state) %></p>
It should be :
<p>Add a city: <%= link_to "Add city", new_state_cities_path(#state) %></p>
This is a language nuance, that takes some getting used to. I actually had the same problem. The paths need to be pluralized. I would also check to make sure that your routes.rb file has the pluralized version as well. There should be a line that looks like this:
map.resources :cities
If you have a line that says city instead of cities you should change it to cities. Hope this helps. Another great resource to check out is the #ruby irc channel on freenode, if you run into anymore problems.
Nevermind - I think I figured it out... I needed to have cities defined as a resource on its own, as well as a sub-resource of states. Now it seems to work.