将Rails表单部分嵌入到另一个页面中 [英] Embed a Rails form partial into another page

查看:142
本文介绍了将Rails表单部分嵌入到另一个页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个带有联系我们页面的rails 4.2.0应用程序(此页面确实有一个半空控制器)。我试图从另一个控制器嵌入一个表单部分。



以下是代码(减去文字):

 < ;%如果user_signed_in? %GT; 
<%render'queries / form'%>
<%end%>

当我运行这个时,我得到错误'表单中的第一个参数不能包含零或为空。



我的查询表单看起来像一个基本的栏杆形式:

 < ;%= form_for @enquiry do | f | %GT; 
<%如果@ enquiry.errors.any? %GT;
< div id =error_explanation>
< h2><%= pluralize(@ enquiry.errors.count,error)%>禁止保存此查询:< / h2>
< ul>
<%@ enquiry.errors.full_messages.each do | message | %GT;
< 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,说明:%>< br>
<%= f.text_area:e_description%>
< / div>
< div class =actions>
<%= f.submit%>
< / div>

错误的可能原因是什么?或者有更好的方法将视图嵌入到另一个视图中?



更新/编辑:



这里是路线:

  devise_for:用户

资源:房间做
资源:查看
结束

资源:rmcats
资源:extras
资源:extracats
资源:查询

root:to =>重定向('/ pages / home')

获得'pages / home'
获得'pages / contactus'

和查询控制器:

  class EnquiriesController< ApplicationController 
before_action:set_enquiry,only:[:show,:edit,:update,:destroy]

#GET / queries
def index
@enquiries = Inquiry .all
end

#GET / queries / 1
def show
end

#GET / queries / new
def new
@enquiry = Enquiry.new
end

#GET / queries / 1 / edit
def edit
end

$ POST /查询
def创建
@enquiry = Enquiry.new(enquiry_params)

如果@ enquiry.save
redirect_to @enquiry,notice:'Inquiry '
else
render:new
end
end

#PATCH / PUT / queries / 1
def update
if @ enquiry.update(enquiry_params)
redirect_to @enquiry,注意:'Inquiry has successfully updated。'
else
render:edit
end
end

#DELETE / queries / 1
def destroy
@ enquiry.destroy
redirect_to enquiries_u rl,notice:'Inquiry was destroyed destroyed。'
end

private
#使用回调来共享操作之间的常见设置或约束。
def set_enquiry
@enquiry = Enquiry.find(params [:id])
end

#只允许通过一个可信参数白名单。
def enquiry_params
params.require(:inquiry).permit(:subject,:e_description)
end
end

这是pages控制器:

  class PagesController< ApplicationController 
around_filter:resource_not_found
#def home
#end
private
#如果找不到资源重定向到root和flash错误。
#=>对于页面而言,这将很少需要,因为它应该是404.
def resource_not_found
yield
rescue ActiveRecord :: RecordNotFound
redirect_to root_url,:notice => 找不到网页。
end
end

编辑:

Log:

 开始GET/ pages / contactus为:: 1于2015-03-21 01:05:25 +0000 
由EnquiriesController处理#new as HTML
[1m [35mUser Load(0.0ms)[0m SELECTusers。* FROMusersWHEREusers。id =? ORDER BYusers。idASC LIMIT 1 [[id,1]]
渲染查询/ _form.html.erb(0.0ms)
渲染页面/ contactus.html.erb布局/应用程序(0.0ms)
在235ms内完成200 OK(查看:234.6ms | ActiveRecord:0.0ms)


解决方案

它告诉你,当它试图呈现表单时, @enquiry 是零。您需要调用操作来为要表示的表单创建 @enqiury



您可以将您的路线更改为:

  get'pages / contactus'=> '查询#新'

然后在您的调查控制器中:

  def new 
@enquiry = Enquiry.new
render'pages / contactus'
end


好的,现在我们结合Friends Systems在他的回答中提出的问题:

 <%if user_signed_in? %GT; 
<%=呈现'查询/表格'查询:@查询%>
<%end%>

现在更改任何 @enquiry 查询形式



这是因为您需要将变量传递给partial。


I'm building a rails 4.2.0 app with a contact us page (this page does have a semi-empty controller). I'm trying to embed a form partial from another controller.

Here is the code (minus the text):

<% if user_signed_in? %>
 <% render 'enquiries/form' %>
<% end %>

When I run this I get the error 'First argument in form cannot contain nil or be empty'.

My enquiries form looks like a basic rails form:

<%= 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>

What could be the possible reason for the error? Or is there a better way of embedding a view into another?

Update/Edit:

Here's the routes:

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'

And the enquiry 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

This is the pages 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

Edit:

Log:

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)

解决方案

It is telling you that @enquiry is nil at the time it is trying to render the form. You need to call the new action to create the @enqiury for the form to represent.

You could change your route to:

get 'pages/contactus' => 'enquiries#new'

Then in your Enquiry controller:

def new
  @enquiry = Enquiry.new
  render 'pages/contactus'
end

EDIT:

Ok, so now we combine what Friends Systems put in his answer:

<% if user_signed_in? %>
  <%= render 'enquiries/form' enquiry: @enquiry %>
<% end %>

And now change any instance of @enquiry in the form to enquiry

This is because you need to pass the variable to the partial.

这篇关于将Rails表单部分嵌入到另一个页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆