How to fix undefined method error in rails? - ruby-on-rails

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

Related

Rails routing params mismatch error in rails

I am adding excel import feature but I am getting error. :id params is getting passed instead of :project_id params.
Below is my code-
index.html.erb
<%= form_tag import_project_stages_path ,multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<%end %>
stage_controller.rb
def import
Stage.import(params[:file])
redirect_to root_url, notice:"Projects imported. "
end
routes.rb
resources :projects do
resources :stages do
collection {post :import}
end
end
error
No route matches {:action=>"import", :controller=>"stages", :id=>"1"},
missing required keys: [:project_id]
Your nested resources will produce a route path that looks like <host>/projects/:project_id/stages. So, you need to include the parent project ID as part of the path in your form_tag. You can do that by including the project as a parameter to the URL helper method:
<%= form_tag import_project_stages_path(#project), multipart: true do %>
...
<%end %>

Rails NoMethodError

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.

NoMethodError in #new - form_for, but resources set?

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.

Rails 3 routing not generating proper paths for new vs edit views

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 %>

Ruby on Rails form_for causing path error

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 %>

Resources