2017-04-23 159 views
0

我试图在另一个局部视图中为模型request包含一个局部视图。Rails:无法在另一个局部视图中使用局部视图

我有一个模型叫做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

我米试着g至包括requests/formstatic_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是其中原始部分是conluded控制器视图,因此基本上具有结构是

products/bigmikepopular113.html.erb - >static_pages/_requestquote.html.erb - >requests/_form.html.erb

我可以做些什么不同的事情来确保我可以嵌入部分?

回答

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

希望能有所帮助。