Reputation: 977
I have converted a front end project to the Rails environment, and I have used Material Design Lite to design the form fields. I am getting the following error:
NoMethodError in Home#new - undefined method `homes_path'
Can anyone help me fix the error?
routes.rb:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get '/home', to: 'home#index'
get '/home/new', to: 'home#new'
get 'home/:id', to: 'home#show'
end
new.html.erb:
<div class="container-div">
<!-- Colored FAB button with ripple -->
<button id="fab" class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect mdl-button--colored">
<i class="material-icons">add</i>
</button>
<div class="demo-card-wide mdl-card mdl-shadow--2dp">
<div class="mdl-card__title" id="text-div">
<h2 id="title-text" class="mdl-card__title-text">CAMPAIGN</h2>
<br>
<br>
<span id="success">Success!</span>
</div>
<div class="mdl-card__supporting-text">
<%= form_for(@home) do |f| %>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<%= f.text_field_tag :campaign_name,nil,:class => "mdl-textfield__input"%>
<label class="mdl-textfield__label" for="campaign_name">Campaign Name</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<%= f.text_field_tag :phone_number_receiver,nil,:class => "mdl-textfield__input",:type => "number" %>
<label class="mdl-textfield__label" for="phone_number_receiver">Phone Number for recipient</label>
<span class="mdl-textfield__error">Input is not a number!</span>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<%= f.text_field_tag :start_date,nil,:class => "mdl-textfield__input"%>
<label class="mdl-textfield__label" for="start_date" id="start-date-label">Enter start date</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<%= f.text_field_tag :end_date,nil,:class => "mdl-textfield__input"%>
<label class="mdl-textfield__label" for="end_date" id="end-date-label">Enter end date</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<%= f.text_field_tag :start_time,nil,:class => "mdl-textfield__input"%>
<label class="mdl-textfield__label" for="start_time" id="start-time-label">Enter time</label>
</div>
<div class="mdl-textfield mdl-js-textfield less-margin mdl-textfield--floating-label">
<%= f.text_area_tag :sms_msg, nil,:class => "mdl-textfield__input", :rows => 8, :cols => 40 %>
<label class="mdl-textfield__label" for="sms_msg">Text Message</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<%= text_field_tag :break_msg,"1",:class => "mdl-textfield__input"%>
<label class="mdl-textfield__label" for="break_msg">Number of Pages</label>
</div>
<%end%>
</div>
</div>
</div>
home_controller.rb:
class HomeController < ApplicationController
def index
@campaigns = Home.all
end
def new
@home = Home.new
end
def show
@campaign = Home.find(params[:id])
end
end
PS: The form field for :break_msg is not a part of the model Home.
Link to github: https://github.com/rimildeyjsr/sms-scheduler
Upvotes: 0
Views: 774
Reputation: 11896
Looks like your form is pointing to a route (homes_path
or /homes
) that doesn't exist. You need to create a POST
route and corresponding controller action.
post '/home', to: 'home#create', as: :homes
Keep in mind, the as: :homes
may be optional. Run rake routes
to verify.
And for the controller action, something like this:
def create
@home = Home.create(home_params)
...
end
For more info, check out the Rails docs on controllers.
EDIT: Also, you're using text_field_tag
and text_area_tag
in your form but since you're using an ActiveRecord object (@home
) then you don't need the _tag
. So, just f.text_field
and f.text_area
.
Lastly, you should use the resources
helper instead of manually creating the "home" routes. Read through the Rails docs on routing for more info and customization options (e.g. getting /home
instead of /homes
when using resources :homes
).
Upvotes: 1
Reputation: 90
this issue seems similar to an issue I had previously: undefined method `wikis_path'
try renaming your controller to homes_controller.rb
. This will create a bunch of errors that you will need to clean up (such as actually changing class HomeController < ApplicationController
to class HomesController < ApplicationController
as well as fixing your routes). This should address the issue of not having the homes_path
route.
Upvotes: 1