Rails形成了新的动作,而不是创建动作

时间:2014-11-18 21:01:07

标签: ruby-on-rails

我知道我错过了一些非常明显的东西,但我只是被卡住了。我有一个提交的表格给我一个错误。

  

LeadsController #new

中的ActiveRecord :: RecordNotFound

我不确定为什么我会收到此错误,我认为这很奇怪,因为提交按钮不应该触发创建操作?

无论如何这是我的控制器

class LeadsController < ApplicationController
    def new
        @daycare = Daycare.all
        @city = []
        @neighborhood = []
        @paginate_info = Daycare.where(city: params[:search]).paginate(page: params[:page], per_page: 12)
        @daycare_info = Hash.new
        @daycare.each do |daycare|
            @city << daycare.city
            @neighborhood << daycare.neighborhood
        end
        @paginate_info.each do |daycare|
            @daycare_info[daycare.yelp_id] = {daycare.name => [daycare.logo, daycare.rating_url, daycare.cross_streets, daycare.address, daycare.id]}
        end
        @city = @city.uniq.sort
        @neighborhood = @neighborhood.flatten.uniq.sort
        @daycare_find = Daycare.find(params[:id])
        @daycare_id = @daycare_find.id
        @daycare_name = @daycare_find.name
        @latitude = @daycare_find.latitude
        @longitude = @daycare_find.longitude
        @address = @daycare_find.address
        @snippet = @daycare_find.review_snippet
        @review_image = @daycare_find.review_user_image
        @rating = @daycare_find.review_image
        @user_name = @daycare_find.review_user_name
        @yelp = @daycare_find.yelp_url
        @phone = @daycare_find.phone
        @parent = Lead.new
    end

    def create
        @parent = Lead.new(parent_params)
        if @parent.save 
            ContactMailer.lead_email(@parent).deliver
            flash[:success] = "Thanks for your interest someone will be contacting you shortly"
            redirect_to root_path
        else
            render 'new'
        end
    end

     private

  def client_params
    params.require(:client).permit(:email, :child_age, :daycare, :details, :phone_number, :name)
  end
end

这是我的观点

<form>
              <fieldset class="form-group">
                  <%= form_for(@parent, controller: "leads_controller", action: "create") do |f| %>
                     <%= f.label :name %>
                     <%= f.text_field :name , class: "form-control" %>
                     <%= f.label :email %>
                     <%= f.text_field :email , class: "form-control" %>
                     <%= f.label :phone_number %>
                     <%= f.text_field :phone_number , class: "form-control" %>
                     <%= f.label :child_age, "Child's Age" %>
                     <%= f.text_field :child_age , class: "form-control" %>
                     <%= f.hidden_field :daycare, value: @daycare_id %>
                     <%= f.label :intended_program, "Intended Program *example: Infant, Toddler, Preschool" %>
                     <%= f.text_field :intended_program, class: "form-control"%>    
                     <%= f.label :details, "Message" %>
                     <%= f.text_area :details , rows: 3, class: "form-control" %>    
                     <%= f.submit "Submit", class: "btn btn-primary" %>
              <% end %>
              </fieldset>

编辑:添加生成的HTML

 <form>
              <fieldset class="form-group">
                  <form accept-charset="UTF-8" action="/leads" class="new_lead" id="new_lead" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="gpC5PlJAM8WLnXi0haDlDKgQGDftnJgYT+Nq94YnDJo=" /></div>
                     <label for="lead_name">Name</label>
                     <input class="form-control" id="lead_name" name="lead[name]" type="text" />
                     <label for="lead_email">Email</label>
                     <input class="form-control" id="lead_email" name="lead[email]" type="text" />
                     <label for="lead_phone_number">Phone number</label>
                     <input class="form-control" id="lead_phone_number" name="lead[phone_number]" type="text" />
                     <label for="lead_child_age">Child&#39;s Age</label>
                     <input class="form-control" id="lead_child_age" name="lead[child_age]" type="text" />
                     <input id="lead_id" name="lead[id]" type="hidden" value="2" />
                     <input id="lead_daycare" name="lead[daycare]" type="hidden" value="2" />
                     <label for="lead_intended_program">Intended Program *example: Infant, Toddler, Preschool</label>
                     <input class="form-control" id="lead_intended_program" name="lead[intended_program]" type="text" />    
                     <label for="lead_details">Message</label>
                     <textarea class="form-control" id="lead_details" name="lead[details]" rows="3">
</textarea>    
                     <input class="btn btn-primary" name="commit" type="submit" value="Submit" />
</form>              </fieldset>

的routes.rb

ChicagoDaycares::Application.routes.draw do
  root "daycares#index"
  get "city/:search" => 'daycares#city', as: :city
  get "neighborhood/:neighborhood_search" => 'daycares#neighborhood', as: :neighborhood
  resources :daycares
  resources :leads
end

1 个答案:

答案 0 :(得分:0)

form_for @parent, :url => leads_path, :method => "post"

此外,您不需要在外面包裹表单标记,form_for会生成自己的表单标记