【问题标题】:How can I implement Javascript/ Jquery in rails 4.2.4 with ruby 2.2.3如何使用 ruby​​ 2.2.3 在 rails 4.2.4 中实现 Javascript/Jquery
【发布时间】:2016-06-19 21:03:40
【问题描述】:

我正在用 ruby​​ on rails 构建一个应用程序。 rails 和 ruby​​ 的版本如上所列。

我的目标是在我的思维导图索引视图中实现 JavaScript/Jquery,以便它们可以动态地将信息添加到我的页面中,而无需重新加载整个页面。所以他们将是一个被点击的链接,它将显示思维导图表单,在提交表单时,表单应该消失并且新数据应该附加到指定的 div 视图中。 --到目前为止,一旦我单击新链接,我就能够显示思维导图表单,但是表单在提交表单时并没有消失。我已经能够允许用户输入和提交信息,但信息仅在我重新加载整页后才会显示。

我尝试了一个数字,但似乎没有任何帮助。

有人请帮忙。如果你住在芝加哥,我欠你一杯啤酒:)。

下面是控制器、js和视图文件。

class MindmapsController < ApplicationController

  before_action :find_capsule, only: [:new, :index, :create, :show, :update, :destroy]
  before_action :find_mindmap, only: [:show, :edit, :index, :update, :destroy]

  before_action :authenticate_author!

  def index
    @mindmaps = Mindmap.all.order('created_at DESC')

  end

  def new
    @mindmap = Mindmap.new
  end

  def create
    @mindmap = Mindmap.new(mindmap_params)
    if @mindmap.save
      respond_to do |format|
        format.html {redirect_to mindmaps_path , notice:"You have successfully created a Mindmap"}
        format.js
      end
    else
      render 'new'
    end
  end

  def show
  end

  def edit
  end

  def update
    if @mindmap.update(mindmap_params)
      redirect_to @mindmap , notice:" You have successfully updated Your Mindmap"
    else
      render 'edit'
    end
  end

  def destroy
    @mindmap.destroy
    redirect_to mindmaps_path
  end

  def find_mindmap
    @mindmap = Mindmap.find_by(params[:id])
  end

  def find_capsule
    @capsule = current_author.capsules.find_by(params[:id])
  end

  def mindmap_params
    params.require(:mindmap).permit(:src, :src_purpose, :capsule_id, :profile_id)
  end
end

JavaScript 文件位于思维导图视图文件夹下仅供参考

new.js.erb

$("a#new_mindmap_link").hide().after("<%= j render('form')%>");

create.js.erb

$("form#new_mindmap").remove();
$("a#new_mindmap_link").show();
$("div#mindmap").append("<%= j render(@mindmaps) %>");

思维导图索引页面

思维导图#index

<%= link_to "Create Mindmap", new_mindmap_path, id:"new_mindmap_link", remote: true %>
<% @mindmaps.each do |mindmap| %>
<div class="mindmap">    
    <div>Source: <%= mindmap.src %></div>
    <div class="meta">
  <%= link_to time_ago_in_words(mindmap.created_at) + " ago" , mindmap %>
        <span class="admin"><%= link_to "Edit", edit_mindmap_path(mindmap) %>
          <%= link_to "Delete", mindmap ,  method: :delete, data: { confirm: "Are you sure you want to delete this Mindmap" } %>
        </span>
    </div>
  </div>
<% end %>

思维导图形式

<div class="col-md-4 mindmap-container">
  <%= simple_form_for @mindmap, remote: true do |f| %>
    <%= f.input :src, as: :url, class:'form-control', label: false, placeholder:"Add a URL" %>
    <%= f.input :src_purpose, as: :text, class: 'form-control', label: false,  placeholder: "Add a description to your URL"%>
    <%= f.input :capsule_id, label: false, collection: Capsule.all, default: 'Select a capsule', label_method: :title %>
    <%= f.input :profile_id, label: false, collection: Profile.all, default: 'Select a Profile ID',label_method: :profile_name %>
    <%= f.button :submit, class:'btn btn-primary' %>
    <% end %>
</div>

【问题讨论】:

  • 你在这里展示了很多代码。您能否缩小问题发生的范围并更详细地描述它?
  • 我的 javascript 有问题。这是我根据在 youtube 上找到的教程尝试做的事情,这里是链接。 youtube.com/watch?v=FBxVN7U1Qsk,我正在尝试完全做到这一点,但基本上是在这个控制器的索引页面上。
  • 听起来一切都在您的代码中正常工作,只是需要重新加载页面才能运行一些 jQuery。这可能是 Turbolinks 问题。尝试removing Turbolinks 或查看this answer 了解如何避免使用 Turbolinks 时出现$(ready 问题。

标签: javascript jquery html ruby-on-rails ruby


【解决方案1】:

如果您检查日志,我怀疑是否调用了 js.erb 视图,因为您的操作目前只知道响应 html。我认为解决此问题的一种方法是在您的创建操作中添加一个 respond_to

def create
    @muse = Muse.new(muse_params)
    @muse.author_id = current_author.id
    if @muse.save
      respond_to do |format|
         format.html { redirect_to @muse, notice: "You have successfully added your muses" }
         format.json
      end
    else
      render :new
    end
  end

让我知道这是否有帮助

【讨论】:

  • 抱歉,堆栈溢出似乎在更新我的文件时出现问题。刚刚更新了正确的控制器代码。让我知道你的想法。
  • 不,表单不会消失,一旦提交并且我最初隐藏的链接不会出现。新内容未附加到思维导图 div
  • 你有这个在公共 Github 上吗?
  • 不,我没有。这个项目在比特桶中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多