Rails: 无法在另一个分部视图中使用分部
Rails: Unable to use partial in another partial view
我正在尝试在另一个部分视图中包含模型 request
的部分形式。
我有一个模型叫做 request
。
class CreateRequests < ActiveRecord::Migration[5.0]
def change
create_table :requests do |t|
t.string :name
t.string :email
t.string :phone
t.string :product
t.string :details
t.timestamps
end
end
end
我有我的requests_controller.rb
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
@requests = Request.all
end
# GET /requests/1
# GET /requests/1.json
def show
end
# GET /requests/new
def new
@request = Request.new
end
# GET /requests/1/edit
def edit
end
# POST /requests
# POST /requests.json
def create
# Create the user from params
@request = Request.new(request_params)
if @email.save
# Deliver the signup email
RequestNotifierMailer.send_email(@request).deliver
flash[:success] = "Thanks! We'll be in touch soon!"
redirect_to :action => 'new'
else
render :action => 'new'
end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
respond_to do |format|
if @request.update(request_params)
format.html { redirect_to @request, notice: 'Request was successfully updated.' }
format.json { render :show, status: :ok, location: @request }
else
format.html { render :edit }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
# DELETE /requests/1
# DELETE /requests/1.json
def destroy
@request.destroy
respond_to do |format|
format.html { redirect_to requests_url, notice: 'Request was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
@request = Request.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:name, :email, :phone, :product, :details)
end
end
我有部分表格requests/_form.html.erb
<%= form_for(request) do |f| %>
<% if request.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(request.errors.count, "error") %> prohibited this request from being saved:</h2>
<ul>
<% request.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :phone %>
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :product %>
<%= f.text_field :product %>
</div>
<div class="field">
<%= f.label :details %>
<%= f.text_field :details %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我通过添加以下内容将资源包含在我的 config/routes.rb
中:
resources :requests
我正在尝试将 requests/form
包含到 static_pages/_requestquote.html.erb
中:
<div class="actionsHolder">
<div class="buyHolder">
<h2>Starting from <%= price %></h2>
<h3 id="myBtn">
<a href="#">
Request for Quote
</a>
</h3>
</div>
</div>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<%= render :partial => "requests/form" %>
</div>
</div>
但是我收到错误:
NoMethodError in Products#bigmikepopular113
Showing /Users/beckah/Documents/projects/Envirovacs/app/views/requests/_form.html.erb where line #1 raised:
undefined method `model_name' for #<ActionDispatch::Request:0x007fee89b49040>
Extracted source (around line #1):
1
2
3
4
5
6
<%= form_for(request) do |f| %>
<% if request.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(request.errors.count, "error") %> prohibited this request from being saved:</h2>
<ul>
Products#bigmikepopular113
是包含原始部分的控制器视图,因此结构本质上是
products/bigmikepopular113.html.erb
-> static_pages/_requestquote.html.erb
-> requests/_form.html.erb
我可以做些什么来确保我可以有一个嵌入的部分?
据我所知,您没有在任何地方初始化新请求的创建。仅在控制器内的新操作下,但没有与该操作对应的视图。您只有视图 _form.html.erb.
澄清一下:控制器中的每个动作都必须对应于一个视图,与控制器内的动作共享相同的名称。即使是部分的。
因此,def new
自动指向 new.html.erb。 def index
到 index.html.erb 等等。
因此你有两个选择。在控制器中创建一个 def form
,并相应地在 routes.rb 中设置路由,或者直接在视图中而不是在控制器中直接编写语句:
删除:
<%= form_for(request) do |f| %>
改为:
<%= form_for Request.new do |f| %>
希望对您有所帮助。
我正在尝试在另一个部分视图中包含模型 request
的部分形式。
我有一个模型叫做 request
。
class CreateRequests < ActiveRecord::Migration[5.0]
def change
create_table :requests do |t|
t.string :name
t.string :email
t.string :phone
t.string :product
t.string :details
t.timestamps
end
end
end
我有我的requests_controller.rb
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
@requests = Request.all
end
# GET /requests/1
# GET /requests/1.json
def show
end
# GET /requests/new
def new
@request = Request.new
end
# GET /requests/1/edit
def edit
end
# POST /requests
# POST /requests.json
def create
# Create the user from params
@request = Request.new(request_params)
if @email.save
# Deliver the signup email
RequestNotifierMailer.send_email(@request).deliver
flash[:success] = "Thanks! We'll be in touch soon!"
redirect_to :action => 'new'
else
render :action => 'new'
end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
respond_to do |format|
if @request.update(request_params)
format.html { redirect_to @request, notice: 'Request was successfully updated.' }
format.json { render :show, status: :ok, location: @request }
else
format.html { render :edit }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
# DELETE /requests/1
# DELETE /requests/1.json
def destroy
@request.destroy
respond_to do |format|
format.html { redirect_to requests_url, notice: 'Request was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
@request = Request.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:name, :email, :phone, :product, :details)
end
end
我有部分表格requests/_form.html.erb
<%= form_for(request) do |f| %>
<% if request.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(request.errors.count, "error") %> prohibited this request from being saved:</h2>
<ul>
<% request.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :phone %>
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :product %>
<%= f.text_field :product %>
</div>
<div class="field">
<%= f.label :details %>
<%= f.text_field :details %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我通过添加以下内容将资源包含在我的 config/routes.rb
中:
resources :requests
我正在尝试将 requests/form
包含到 static_pages/_requestquote.html.erb
中:
<div class="actionsHolder">
<div class="buyHolder">
<h2>Starting from <%= price %></h2>
<h3 id="myBtn">
<a href="#">
Request for Quote
</a>
</h3>
</div>
</div>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<%= render :partial => "requests/form" %>
</div>
</div>
但是我收到错误:
NoMethodError in Products#bigmikepopular113
Showing /Users/beckah/Documents/projects/Envirovacs/app/views/requests/_form.html.erb where line #1 raised:
undefined method `model_name' for #<ActionDispatch::Request:0x007fee89b49040>
Extracted source (around line #1):
1
2
3
4
5
6
<%= form_for(request) do |f| %>
<% if request.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(request.errors.count, "error") %> prohibited this request from being saved:</h2>
<ul>
Products#bigmikepopular113
是包含原始部分的控制器视图,因此结构本质上是
products/bigmikepopular113.html.erb
-> static_pages/_requestquote.html.erb
-> requests/_form.html.erb
我可以做些什么来确保我可以有一个嵌入的部分?
据我所知,您没有在任何地方初始化新请求的创建。仅在控制器内的新操作下,但没有与该操作对应的视图。您只有视图 _form.html.erb.
澄清一下:控制器中的每个动作都必须对应于一个视图,与控制器内的动作共享相同的名称。即使是部分的。
因此,def new
自动指向 new.html.erb。 def index
到 index.html.erb 等等。
因此你有两个选择。在控制器中创建一个 def form
,并相应地在 routes.rb 中设置路由,或者直接在视图中而不是在控制器中直接编写语句:
删除:
<%= form_for(request) do |f| %>
改为:
<%= form_for Request.new do |f| %>
希望对您有所帮助。