覆盖Rails远程表单提交中使用的JavaScript模板 [英] Overriding the JavaScript template used in a Rails remote form submission

查看:72
本文介绍了覆盖Rails远程表单提交中使用的JavaScript模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个似乎没有触发预期命名的.js.erb文件的Rails应用程序,所以我想覆盖默认行为,但我不确定如何。



详细信息:



这是一个Rails 3应用程序,它通过AJAX调用呈现button_to以切换状态后端,然后修改按钮的文本,以反映再次单击它时反向操作。基本上保存至计划变为从计划中删除,您可以再次点击它以执行相反的操作。



这实际上工作正常,但只有我放了jQuery我想执行show.js.erb(以与显示按钮所在页面的方法相关联的视图命名)而不是toggle.js.erb(基于被调用的实际方法的名称)。 / p>

在这个例子中,我使用的是jquery-rails。



从呈现按钮的视图( show.html.erb (更具体地说,从那里调用的部分):

 <%if current_user.shows.include?(@ show)%> 
< p><%= button_to从计划中删除,toggle_show_path(@show),:remote => true ,:class =>'schedule_button'%>
<%else%>
< p><%= button_to保存至计划,toggle_show_path(@show),: remote => true,:class =>'schedule_button'%>
<%end%>

来自 routes.rb

 匹配'/ shows / toggle /:id'=> 'show#toggle',:as => :toggle_show 

来自我的`shows_controller.rb:

  def toggle 
if current_user
show = Show.find(params [:id])
if show
if current_user.shows .include?(显示)
current_user.shows.delete(显示)
flash [:notice] =此节目已从您的日程安排中移除。
else
current_user.shows<< show
flash [:notice] =此节目已添加到您的日程表中。
结束
其他
flash [:error] =未知节目。
end
else
flash [:error] =你还没有登录。
结束
结束

我想这里有一些默认约定指示要使用哪个.js.erb文件,但为什么不使用基于表单提交的方法的文件?



我一直在试验明确告诉它该怎么做,但我不太确定如何指定要调用的备用.js.erb文件的名称。这似乎不是这样:

  respond_to do | format | 
format.html {redirect_to show_path(params [:id])}
format.js {render toggle_show_path(params [:id])}
end

从我的日志中:

  ActionView: :MissingTemplate(缺少模板/显示/切换/ 22,{:handlers => [:erb,:rjs,:builder,:rhtml,:rxml] ,: formats => [:js,:html],:locale => [:en,:en]}在视图路径/Data/Sites/tvgridthing.local/app/views,/ Users / jschuur / .rvm / gems / ruby​​-1.9.2-p0 / gems / devise-1.1.3 / app / views,/ data / Site / tvgridthing.local,/):
app / controllers / shows_controller.rb:78:在切换中的'block(2个级别) '
app / controllers / shows_controller.rb:76:在'切换'

我'我假设因为我想要覆盖默认行为,我不能再使用Rails 3更短的'respond_with格式?



无关的问题:任何方式来浓缩我添加/的逻辑将节目移除到我的用户模型?我正在做一个查找,一个包含和添加/删除,并想知道是否有一个更简单的解决方案。



更新:我只想到这个在其他人的帮助下,我发送此链接。解决方案很简单:渲染:切换



如果有任何关于优化切换逻辑的建议仍然很好奇。

解决方案

如果你改变了

  format.js {render toggle_show_path(params [:id])} 

To just

  format.js 

默认情况下,它会在文件夹视图/节目中显示为名为 toggle.js.erb 的文件,因为您在控制器中的方法切换内。



如果您在方法展示中,那么您的唯一解决方案是

 渲染:切换

  render:action => 'toggle'

希望这会有所帮助


I've got a Rails app that doesn't seem to be triggering the expectedly named .js.erb file, so I'd like to override the default behavior, but I'm not sure how.

The details:

It's a Rails 3 app that renders a button_to with an AJAX call to toggle the status of something on the backend and then modifies the text of the button to reflect the reverse action if you click on it again. Basically 'Save to schedule' becomes 'Remove from schedule' and you can click on it again to do the opposite.

This actually works fine, but only if I put the jQuery I want executed into show.js.erb (named after the view associated with the method that shows the page where the button is on) and not toggle.js.erb (based on the name of the actual method being called).

In this example, I'm using jquery-rails.

From the view that renders the button (show.html.erb (more specifically, a partial called from there):

<% if current_user.shows.include?(@show) %>
  <p><%= button_to "Remove from schedule", toggle_show_path(@show), :remote => true, :class => 'schedule_button' %>
<% else %>
  <p><%= button_to "Save to schedule", toggle_show_path(@show), :remote=> true, :class => 'schedule_button' %>
<% end %>

From routes.rb:

match '/shows/toggle/:id' => 'shows#toggle', :as => :toggle_show

From my `shows_controller.rb:

def toggle
  if current_user
    show = Show.find(params[:id])
    if show
      if current_user.shows.include?(show)
        current_user.shows.delete(show)
        flash[:notice] = "This show has been removed from your schedule."
      else
        current_user.shows << show
        flash[:notice] = "This show has been added to your schedule."
      end
    else
      flash[:error] = "Unknown show."
    end
  else
    flash[:error] = "You're not logged in."
  end
end

I imagine there are some defaults conventions going on here that dictate which .js.erb file to use, but why isn't it using the one based on the method that the form submitted to?

I've been experimenting with telling it explicitly what to do, but I'm not quite sure how to specify the name of my alternate .js.erb file to be invoked. This doesn't seem to be it:

respond_to do |format|
  format.html { redirect_to show_path(params[:id]) } 
  format.js { render toggle_show_path(params[:id]) }
end

From my log:

ActionView::MissingTemplate (Missing template /shows/toggle/22 with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :html], :locale=>[:en, :en]} in view paths "/Data/Sites/tvgridthing.local/app/views", "/Users/jschuur/.rvm/gems/ruby-1.9.2-p0/gems/devise-1.1.3/app/views", "/Data/Sites/tvgridthing.local", "/"):
  app/controllers/shows_controller.rb:78:in `block (2 levels) in toggle'
  app/controllers/shows_controller.rb:76:in `toggle'

I'm assuming since I want to override the default behavior, I can no longer use Rails 3's shorter 'respond_with format?

Unrelated question: Any way to condense my logic for adding/removing the show to my user model? I'm doing a find, an include and and add/delete and wonder if there's a simpler solution.

Update: I just figured this out with the help of someone else I sent this link to. The solution was simply: render :toggle.

Still curious if any has suggestions about optimizing the toggle logic.

解决方案

If you change

format.js { render toggle_show_path(params[:id]) }

To just

format.js

It will go by default to the file called toggle.js.erb in the folder views/shows since you are inside the method toggle in the controller.

If you are inside the method show then yes your only solution is

render :toggle

Or

render :action => 'toggle'

Hope this helps

这篇关于覆盖Rails远程表单提交中使用的JavaScript模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆