Rails - Home#new中的NoMethodError

时间:2017-01-23 18:29:05

标签: ruby-on-rails

我已经将前端项目转换为Rails环境,并且我使用Material Design Lite来设计表单字段。我收到以下错误:

Home#new中的NoMethodError - 未定义的方法`homes_path'

任何人都可以帮我解决错误吗?

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::break_msg的表单字段不是模型Home的一部分。

链接到github:https://github.com/rimildeyjsr/sms-scheduler

2 个答案:

答案 0 :(得分:1)

您的表单看起来指向一条不存在的路线(homes_path/homes)。您需要创建POST路由和相应的控制器操作。

post '/home', to: 'home#create', as: :homes

请注意,as: :homes可能是可选的。运行rake routes进行验证。

对于控制器动作,这样的事情:

def create
  @home = Home.create(home_params)
  ...
end

有关详细信息,请查看Rails docs on controllers

编辑:此外,您在表单中使用了text_field_tagtext_area_tag,但是因为您正在使用ActiveRecord对象({{1} })那么你不需要@home。所以,只需_tagf.text_field

最后,您应该使用f.text_area帮助程序而不是手动创建&#34; home&#34;路线。请仔细阅读Rails docs on routing了解更多信息和自定义选项(例如,在使用resources时获取/home而不是/homes

答案 1 :(得分:1)

此问题与我之前遇到的问题类似:undefined method `wikis_path'

尝试将控制器重命名为homes_controller.rb。这将产生一系列您需要清理的错误(例如实际将class HomeController < ApplicationController更改为class HomesController < ApplicationController以及修复您的路线)。这应该解决没有homes_path路线的问题。