Rails:无法在另一个局部视图中使用partial

时间:2017-04-23 18:19:10

标签: ruby-on-rails views

我正在尝试在另一个局部视图中为模型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">&times;</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 - &gt; static_pages/_requestquote.html.erb - &gt; requests/_form.html.erb

我可以做些什么来确保我可以嵌入部分?

1 个答案:

答案 0 :(得分:2)

从我所看到的,你没有在任何地方初始化新请求的创建。仅在控制器内的新操作下,但没有与该操作对应的视图。您只有视图_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| %>

希望这会有所帮助。