简体   繁体   中英

Rails: Unable to use partial in another partial view

I'm trying to include a form partial for the model request in another partial view.

I have a model called 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 

I have my 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

The form partial I have 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 %>

And I included the resource in my config/routes.rb by adding: resources :requests

I'm trying to include requests/form into 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>

However I'm getting the error:

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 is the controller view where the original partial is conluded, so essentially the structure is

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

What can I do differently to make sure I can have an embedded partial?

From what I can see, you're not initializing the creation of a New request anywhere. Only under the new action inside your controller, but there's no view corresponding to that action. You only have the view _form.html.erb.

To clarify: every action in your controller, must correspond to a view, sharing the same name as the action inside the controller. Even if it's a partial.

So, def new automatically points to new.html.erb. def index to index.html.erb and so on.

You therefor have two options. Either create a def form inside the controller, and set up the routes in your routes.rb accordingly, or simply write the statement directly inside the view instead of in the controller:

Remove:

<%= form_for(request) do |f| %>

Instead:

<%= form_for Request.new do |f| %>

Hope that'll help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM