我不太明白为什么这不起作用。不知怎的,我的服务ID根本没有通过。我认为我对ID感到困惑,但这应该有效,但不是。当我点击提交时,它会告诉我:找不到具有' id' =
的服务这是我的代码:
enquires_controller.rb
class EnquiresController < ApplicationController
before_action :set_enquire, only: [:create, :new]
def new
@enquire = Enquire.new
end
def create
@enquire = Enquire.new(enquire_params)
@enquire.service = @service
if user_signed_in?
@user = current_user
end
if @enquire.save
flash[:thanks] = "Thank you"
redirect_to service_path(@service)
else
render :new
end
end
private
def set_enquire
@service = Service.find(params[:service_id])
end
def enquire_params
params.require(:enquire).permit(:first_name, :last_name, :company,
:message, :email, :phone_number,
:service_id )
end
enquire.rb
class Enquire < ApplicationRecord
belongs_to :service
validates :first_name, presence: true
validates :last_name, presence: true
validates :company, presence: true
validates :message, presence: true
validates :email, presence: true
validates :service_id, presence: true
end
使用@service的服务视图
show.html.erb
<%= link_to 'Enquire about this Service', new_enquire_path(@service),
class: "btn btn-primary enquire-btn text-middle" %>
service.rb
has_many :enquires, :dependent => :destroy
的routes.rb
resources :services do
resources :enquires, only: [ :new, :create ]
end
resources :enquires, only: [ :new, :create, :index, :show, :destroy ]
并最后查询表格 _form.html.erb
<%= simple_form_for(@enquire) do |f| %>
<% if user_signed_in? %>
<div class="form-inputs" id="session-form">
<%= f.input :first_name, as: "hidden", input_html: { value: @user.first_name } %>
<%= f.input :last_name, as: "hidden", input_html: { value: @user.last_name } %>
<%= f.input :email, as: "hidden", input_html: { value: @user.email } %>
<%= f.input :company, as: "hidden", input_html: { value: @user.company_name } %>
<%= f.input :phone_number, autofocus: true, placeholder: "Phone Number", label:false %>
<%= f.input :message, autofocus: true, :input_html => { id: "no-resize" }, placeholder: "Message", label:false %>
</div>
<% else %>
<div class="form-inputs" id="session-form">
<%= f.input :first_name, required: true, autofocus: true, placeholder: "First Name", label:false %>
<%= f.input :last_name, required: true, autofocus: true, placeholder: "Last Name", label:false %>
<%= f.input :email, required: true, autofocus: true, placeholder: "Email", label:false %>
<%= f.input :company, required: true, autofocus: true, placeholder: "Company", label:false %>
<%= f.input :phone_number, autofocus: true, placeholder: "Phone Number", label:false %>
<%= f.input :message, autofocus: true, :input_html => { id: "no-resize" }, placeholder: "Message", label:false %>
</div>
<% end %>
<div class="row">
<div class="col-xs-12">
<div class="form-actions text-center">
<%= f.button :submit, "Submit Enquiry", class: "gen-create-btn" %>
</div>
</div>
</div>
<% end %>
点击提交时我得到的参数是:
&#34;✓&#34;,&#34; authenticity_token&#34; =&GT;&#34; xcqR44gPOI7gsN47ddnqKxKuTcaB1Pi5goLa5H81oecbDD7nxE / A1J4KR7rY0Dbyndgz6BrFKzukkTbOCjoGMg ==&#34;,&#34;查询&#34; =&GT; { &#34; first_name&#34; =&gt;&#34; daadadad&#34;,&#34; last_name&#34; =&gt;&#34; adadada&#34;,&#34; email&#34; =&gt; ;&#34; dadad&#34;,&#34;公司&#34; =&gt;&#34; adada&#34;,&#34; phone_number&#34; =&gt;&#34; dadad&#34;, &#34;消息&#34; =&gt;&#34; adada&#34;},&#34;提交&#34; =&gt;&#34;提交查询&#34;,&#34;控制器&#34; =&gt;&#34;查询&#34;,&#34;操作&#34; =&gt;&#34;创建&#34;}允许:false&gt;
编辑:
也许服务展示方法与它有关? services_controller.rb
def show
@service = Service.find(params[:id])
@user = current_user
end
答案 0 :(得分:2)
您before_action :set_enquire, only: [:create, :new]
和[{1}}方法会调用new
。
create
当您查看def set_enquire
@service = Service.find(params[:service_id])
end
方法的params
时,没有create
,因此service_id
为params[service_id]
。所以它失败了nil
<强>解决方案:强>
删除Couldn't find Service with 'id'=
方法的before_action
create
并且before_action :set_enquire, only: [:new]
在<{1}}方法中可用,请将@service
作为new
service_id
在hidden_field
方法中,添加以下行
<%= f.hidden_filed :service_id, @service %>
或者
除了create
之外,我还可以看到@service = Service.find(params[:enquire][:service_id])
和resources :enquires
还有嵌套资源,因此您可以使用表格中的那些以下
services
注意:强>
在您定义enquires
而不是<%= simple_form_for [@service, @enquire] do |f| %>
set_enquire
更改为set_service
<强>更新强>
您还应该更改
service
到
enquiry
所以<%= link_to 'Enquire about this Service', new_enquire_path(@service),
class: "btn btn-primary enquire-btn text-middle" %>
正确传递。
答案 1 :(得分:0)
我认为你的问题来自于你的routes.rb中查询资源的两条路线,如果你的对象来自第二个资源:查询它会产生一个错误,因为它无法找到&#34;父母&#34;服务的路线。
resources :services do
resources :enquires, only: [ :new, :create ]
end
resources :enquires, only: [ :new, :create, :index, :show, :destroy ]