Create.js.erb Javascript要求部分

时间:2022-02-20 19:38:24

I'm trying to get my my create action to work in my rails app. When I have these two lines

我正在尝试让我的创建操作在我的rails应用程序中运行。当我有这两行

$('#pit_form').remove(); //remove form
$('#new_link').show();  //show new link again

it functions properly, and removes the form plus adds the link back, but I can't show the new pit with my escape. I've tried a few things but it always gives me this error

它运行正常,并删除表格加上添加链接,但我不能用我的逃生显示新的坑。我尝试了一些东西,但它总是给我这个错误

ActionView::Template::Error (Missing partial pits/_pit with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
  * "/Users/markhustad/projects/fire_2/app/views"
  * "/usr/local/rvm/gems/ruby-2.1.1/gems/devise-3.2.4/app/views"
):
    1: $('#pit_form').remove(); //remove form
    2: $('#new_link').show();  //show new link again
    3: $('#pit_index').append(<%= j (render(@pit)) %>);
  app/views/pits/create.js.erb:3:in `_app_views_pits_create_js_erb___3365989432298988625_2214355520'
  app/controllers/pits_controller.rb:20:in `create'

when I add this line or something similar(not sure why I need _pit partial)

当我添加这一行或类似的东西(不知道为什么我需要_pit部分)

$('#pit_index').append(<%= j (render(@pit)) %>);

My pits controller currently

我的坑控制器目前

class PitsController < ApplicationController
  before_action :current_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy

def new
  @pit = Pit.new
end

def index
  @pit = Pit.all
  @user = User.find_by(params[:id])
  @pits = Pit.paginate(:page => params[:page]).order('created_at DESC').group_by { |pit| pit.created_at.strftime("%B %Y") }
end




def create
  @pit = current_user.pits.create(pit_params)
      respond_to do |format|
        format.html { redirect_to pits_path}
        format.js
  end
end



def show
  @pit = Pit.find(params[:id])
end

def edit
end

def update
   @pit = Pit.find(pit_params[:id])
     if @pit.update_attributes(pit_params)
       redirect_to @pit
     end
end

def destroy
  @pit = Pit.find(params[:id])
  @pit.destroy
end


def upvote
  @pit = Pit.find(params[:pit_id])
  @pit.upvote_from current_user
  redirect_to pit_path(@pit)
end

def downvote
  @pit = Pit.find(params[:pit_id])
  @pit.downvote_from current_user
  redirect_to pit_path(@pit)
end


private

def correct_user
    @pit = current_user.pits.find_by_id(params[:id])
    redirect_to root_path if @pit.nil?
  end

def pit_params
    params.require(:pit).permit(:topic, :summary, :image, :video_url, :author, :user_id)
end

end

Pits index

<div class = "container list-pits"> 
  <%= link_to "Add New Pit", new_pit_path, id: "new_link", remote: true, class: "btn btn-default" %>
  <br>
  <br>
  <% @pit.each do |pit| %>

  <div class = "container">
    <div class = "well", id = "pit_index"> 
       <h3 id="pit-title"><%= link_to pit.topic, pit_path(pit) %></h3>
       <p>by <%= link_to pit.author, '#' %></p>
          <br>
            <p><%= pit.summary %></p>
            <p>Replies (<%= pit.comments.count %>)</p>
          <br>

            <p>Pit Created by: <%= link_to pit.user.name, pit.user %> on <%= pit.created_at %></p>

            <%= link_to "View Pit", pit_path(pit), class: "btn btn-primary" %>
            <%= link_to "Delete Pit", pit_path(pit), remote: true, method: :delete, data: { confirm: 'Are you sure?' }  %>
      </div>
    </div>
      <% end %>


 </div>

_form in Pits

在坑中形成_form

<div class="container new-pit">

<%= render 'devise/shared/error_messages', obj: @pit %>

<%= form_for @pit, remote: true, :html => {:multipart => true} do |f| %>

   <div class = "form-horizontal", id = "pit_form">

      <div class="form-group">
                   <%= f.label :topic, class: "col-sm-2 control-label" %>
             <div class="col-sm-6">
                   <%= f.text_field :topic, class: "form-control", autofocus: true %>
             </div>  
      </div>

      <div class="form-group">
                   <%= f.label :author, class: "col-sm-2 control-label" %>
             <div class="col-sm-6">
                   <%= f.text_field :author, class: "form-control", :placeholder => "Enter name of book author, lecturer, or presenter" %>
             </div>  
      </div>

           <div class="form-group">
                   <%= f.label :summary, class: "col-sm-2 control-label" %>
             <div class="col-sm-6">
                   <%= f.text_area :summary, class: "form-control", :placeholder => "Present an argument for or against the material presented and state why you think it is accurate or not" %>
             </div>  
           </div>

           <div class="form-group">
                   <%= f.label :image, class: "col-sm-2 control-label" %>
             <div class="col-sm-6">
                   <%= f.file_field :image, class: "form-control" %>
             </div>  
           </div>
           <div class="form-group">
                   <%= f.label :video, class: "col-sm-2 control-label" %>
             <div class="col-sm-6">
                   <%= f.text_field :video_url, class: "form-control", :placeholder => "example: //www.youtube.com/embed/hBHYdK9xtYs (video is optional)" %>
                   <p class = "youtube-instruction">Must use the shared -> embed option</p>
             </div>  
           </div>
          <div class="form-group">
                <div class="col-sm-offset-2 col-sm-6">
                      <%= f.submit "Start Pit", class: "btn btn-primary" %>
               </div>
          </div>
        </div>
      </div>
  </div>



<% end %>

I know I'm close here but need a bit more direction. Thanks.

我知道我在附近但需要更多方向。谢谢。

1 个解决方案

#1


0  

You need two things. First, a pit.js.erb file that has:

你需要两件事。首先,pit.js.erb文件具有:

$('#pit_form').remove();
$('#new_link').show();
$('#pit_index').append(<%= j (render(@pit)) %>);

I think you already have this.

我想你已经有了这个。

You also need a _pit.html.erb file under the /pits folder that describes the layout of individual pit items on the pit index.

您还需要/ pits文件夹下的_pit.html.erb文件,该文件描述了坑索引上各个坑项的布局。

The order of operations goes like this:

操作顺序如下:

  1. Send a create request via Ajax. On forms, this is typically done with the remote: true option, which uses Unobstrusive JavaScript.
  2. 通过Ajax发送创建请求。在表单上,​​这通常使用remote:true选项完成,该选项使用Unobstrusive JavaScript。

  3. Process the response in the controller's create action using format.js. By default, this renders the .js.erb file that corresponds to the action name (i.e. create.js.erb).
  4. 使用format.js处理控制器创建操作中的响应。默认情况下,这会呈现与操作名称对应的.js.erb文件(即create.js.erb)。

  5. Inside this js.erb file, you can also embed more render commands. In your specific case, you are trying to render a _pit.html.erb or a _pit.js.erb template, both of which are missing.
  6. 在这个js.erb文件中,您还可以嵌入更多渲染命令。在您的特定情况下,您尝试呈现_pit.html.erb或_pit.js.erb模板,这两个模板都缺失。

If you already have the pit template in a specific folder, you can specify it in the render action, like this:

如果您已在特定文件夹中具有凹坑模板,则可以在渲染操作中指定它,如下所示:

$('#pit_index').append("<%= j (render 'myfolder/pit') %>");

I hope this helps.

我希望这有帮助。

#1


0  

You need two things. First, a pit.js.erb file that has:

你需要两件事。首先,pit.js.erb文件具有:

$('#pit_form').remove();
$('#new_link').show();
$('#pit_index').append(<%= j (render(@pit)) %>);

I think you already have this.

我想你已经有了这个。

You also need a _pit.html.erb file under the /pits folder that describes the layout of individual pit items on the pit index.

您还需要/ pits文件夹下的_pit.html.erb文件,该文件描述了坑索引上各个坑项的布局。

The order of operations goes like this:

操作顺序如下:

  1. Send a create request via Ajax. On forms, this is typically done with the remote: true option, which uses Unobstrusive JavaScript.
  2. 通过Ajax发送创建请求。在表单上,​​这通常使用remote:true选项完成,该选项使用Unobstrusive JavaScript。

  3. Process the response in the controller's create action using format.js. By default, this renders the .js.erb file that corresponds to the action name (i.e. create.js.erb).
  4. 使用format.js处理控制器创建操作中的响应。默认情况下,这会呈现与操作名称对应的.js.erb文件(即create.js.erb)。

  5. Inside this js.erb file, you can also embed more render commands. In your specific case, you are trying to render a _pit.html.erb or a _pit.js.erb template, both of which are missing.
  6. 在这个js.erb文件中,您还可以嵌入更多渲染命令。在您的特定情况下,您尝试呈现_pit.html.erb或_pit.js.erb模板,这两个模板都缺失。

If you already have the pit template in a specific folder, you can specify it in the render action, like this:

如果您已在特定文件夹中具有凹坑模板,则可以在渲染操作中指定它,如下所示:

$('#pit_index').append("<%= j (render 'myfolder/pit') %>");

I hope this helps.

我希望这有帮助。