简体   繁体   English

在Ruby on Rails中进行远程(ajax)调用的正确方法

[英]The right way to do remote (ajax) calls in Ruby on Rails

I'm making a Single Page Application with Ruby on Rails (it's my first ruby project ever so I'm definitely missing a lot of stuff yet). 我正在用Ruby on Rails制作一个单页应用程序(这是我有史以来的第一个ruby项目,因此我肯定还缺少很多东西)。 So I have a side menu with some links and the right part of the page is supposed to hold a container which is meant to be filled with some content of partial pages. 因此,我有一个带有一些链接的侧菜单,该页面的右侧应该包含一个容器,该容器将填充部分页面的某些内容。 The typical menu link I have now looks this way: 我现在使用的典型菜单链接看起来是这样的:

 <%= link_to t('my-groups'), :controller => 'dashboard', :action => 'mygroups', :remote => true %>

I have a dashboard controller, here's the simplified version of it 我有一个仪表板控制器,这是它的简化版

 class DashboardController < ApplicationController
   def mygroups
    respond_to do |format|
      format.js
   end
  end

I have a dashboard template with the container div in it 我有一个仪表板模板,其中包含容器div

<div class="right_col" role="main">
    <h2>This is the default content of center page</h2>
</div>

And here's the routes.rb path for it: 这是它的routes.rb路径:

get 'dashboard/mygroups' => 'dashboard#mygroups'

I also have one partial page alogside with my dashboard template and it's called _mygroups.html.erb and a javascript file mygroups.js.erb which is called as my controller action look at the screenshot of the structure 我的仪表板模板的另一边也是页面的一部分,它名为_mygroups.html.erb和一个名为mygroups.js.erb的javascript文件,称为控制器动作,请查看该结构的屏幕截图 在此处输入图片说明

The contents of this js.erb file are: 该js.erb文件的内容为:

$('.right_col').html("<%= escape_javascript(render(:partial => 'mygroups')) %>");

It all works and the partial contents appear inside the container on link click just fine. 一切正常,部分内容出现在容器中,单击链接就可以了。 But there are still 2 problems I couldn't google the answer for 但是仍然有2个问题我无法在google上找到答案

The questions part: 问题部分:

1) It works with Ajax call but if I simply put this http://localhost:3000/dashboard/mygroups to my browser's navigation line and hit enter, it will give me this error 1)它可以与Ajax调用一起使用,但是如果我只是将此http:// localhost:3000 / dashboard / mygroups放入浏览器的导航行并按Enter,它将给我这个错误

ActionController::UnknownFormat in DashboardController#mygroups ActionController::UnknownFormat DashboardController#mygroups中的ActionController :: UnknownFormat

Extracted source (around line #70): 提取的源(第70行左右):

def mygroups respond_to do |format| def mygroups response_do |格式| format.js end end format.js结束结束

How can I avoid this and just redirect to index in this case? 在这种情况下,如何避免这种情况,而只重定向到索引? I understand that ajax uses POST, but I tried to use post instead of get in routes.rb for this action, and it didn't work at all 我了解ajax使用POST,但我尝试使用post而不是get.routes.rb来执行此操作,但它根本不起作用

2) What if I have a lot of actions for different partial pages, do I have to create a new js.erb file for each action? 2)如果我对不同的部分页面有很多操作,该如何为每个操作创建一个新的js.erb文件呢? Can't it be done in some simplier way with just one file? 不能只用一个文件以某种简单的方式完成它吗?

3) Is it possible to not specify controller and action on this link explicitly? 3)是否可以不明确指定此链接上的控制器和动作?

<%= link_to t('my-groups'), :controller => 'dashboard', :action => 'mygroups', :remote => true %> 

I mean since it's supposed to be a POST ajax request, how come I need to display the url like this http://localhost:3000/dashboard/mygroups to a user? 我的意思是,由于它应该是POST ajax请求,因此我为什么需要向用户显示类似http:// localhost:3000 / dashboard / mygroups的网址

Add format.html in controller like: 在控制器中添加format.html,例如:

 class DashboardController < ApplicationController
   def mygroups
    if request.xhr?
      respond_to do |format|
        format.js
       end
    else 
      redirect_to root_url
    end
  end

you can add url in link_to tag like: 您可以在link_to标记中添加网址,例如:

<%= link_to t('my-groups'), '/dashboard/mygroups', :remote => true %>

Answers to you questions 回答您的问题

  1. When you hit the URL in browser, it sends vanilla HTTP get request(non-ajax) which your controller action is not configured to handle it. 当您在浏览器中单击URL时,它将发送原始HTTP get请求(非ajax),您的控制器操作未配置为处理该请求。 You need to add format.html and template named groups.html.erb where generally you will list all the groups, I guess. 您需要添加format.html和模板命名groups.html.erb其中,一般来说,你会列出所有的组,我猜。

respond_to do |format| format.html format.js end

  1. Ideally you have to create separate file for each action but if you can take something common out of different action then you can move common template code to a partial and render it either in a separate template having something special or from the controller action directly. 理想情况下,您必须为每个动作创建单独的文件,但是如果您可以从不同动作中取出一些通用的东西,则可以将通用模板代码移至部分代码,并在具有特殊功能的单独模板中或直接从控制器动作中呈现它。

  2. Yes. 是。 The rails way is to use routes helper. Rails的方法是使用路由助手。 Run rake routes to list all available routes in your app and find relevant helpers. 运行rake routes以列出应用中所有可用的路线,并找到相关的帮助者。

I would strongly suggest to read the rails guide to understand how everything works. 我强烈建议您阅读Rails指南,以了解一切工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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