我正在构建一个带有联系我们页面的 Rails 4.2.0 应用程序(此页面确实有一个半空 Controller )。我正在尝试嵌入来自另一个 Controller 的部分表单。
这是代码(减去文本):
<% if user_signed_in? %>
<% render 'enquiries/form' %>
<% end %>
当我运行它时,出现错误“表单中的第一个参数不能包含 nil 或为空”。
我的查询表单看起来像一个基本的 Rails 表单:
<%= form_for @enquiry do |f| %>
<% if @enquiry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@enquiry.errors.count, "error") %> prohibited this enquiry from being saved:</h2>
<ul>
<% @enquiry.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :subject, "Subject:" %><br>
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :e_description, "Description:" %><br>
<%= f.text_area :e_description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
错误的可能原因是什么?或者是否有更好的方法将一个 View 嵌入到另一个 View 中?
更新/编辑:
路线如下:
devise_for :users
resources :rooms do
resources :viewings
end
resources :rmcats
resources :extras
resources :extracats
resources :enquiries
root :to => redirect('/pages/home')
get 'pages/home'
get 'pages/contactus'
和查询 Controller :
class EnquiriesController < ApplicationController
before_action :set_enquiry, only: [:show, :edit, :update, :destroy]
# GET /enquiries
def index
@enquiries = Enquiry.all
end
# GET /enquiries/1
def show
end
# GET /enquiries/new
def new
@enquiry = Enquiry.new
end
# GET /enquiries/1/edit
def edit
end
# POST /enquiries
def create
@enquiry = Enquiry.new(enquiry_params)
if @enquiry.save
redirect_to @enquiry, notice: 'Enquiry was successfully created.'
else
render :new
end
end
# PATCH/PUT /enquiries/1
def update
if @enquiry.update(enquiry_params)
redirect_to @enquiry, notice: 'Enquiry was successfully updated.'
else
render :edit
end
end
# DELETE /enquiries/1
def destroy
@enquiry.destroy
redirect_to enquiries_url, notice: 'Enquiry was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_enquiry
@enquiry = Enquiry.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def enquiry_params
params.require(:enquiry).permit(:subject, :e_description)
end
end
这是页面 Controller :
class PagesController < ApplicationController
around_filter :resource_not_found
# def home
# end
private
# If resource not found redirect to root and flash error.
# => For pages this will rarely be needed as it should 404.
def resource_not_found
yield
rescue ActiveRecord::RecordNotFound
redirect_to root_url, :notice => "Page not found."
end
end
编辑:
日志:
Started GET "/pages/contactus" for ::1 at 2015-03-21 01:05:25 +0000
Processing by EnquiriesController#new as HTML
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered enquiries/_form.html.erb (0.0ms)
Rendered pages/contactus.html.erb within layouts/application (0.0ms)
Completed 200 OK in 235ms (Views: 234.6ms | ActiveRecord: 0.0ms)
最佳答案
它告诉您 @enquiry
在它试图呈现表单时为 nil。您需要调用 new
操作来为要表示的表单创建 @enqiury
。
您可以将路线更改为:
get 'pages/contactus' => 'enquiries#new'
然后在您的查询 Controller 中:
def new
@enquiry = Enquiry.new
render 'pages/contactus'
end
编辑:
好的,现在我们结合 Friends Systems 在他的回答中给出的内容:
<% if user_signed_in? %>
<%= render 'enquiries/form' enquiry: @enquiry %>
<% end %>
现在将表单中的任何 @enquiry
实例更改为 enquiry
这是因为您需要将变量传递给局部。
关于ruby-on-rails - 将 Rails 表单部分嵌入另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29177803/