1

我希望我的页面打开一个模态弹出框,所以我将此链接添加到我的视图文件中:

  <%= link_to_remote '+ Add Discount', :url => {:controller => "parent_wise_fee_payments", :action => "new_instant_discount", :id => @financefee.id, :current_action => @target_action, :current_controller => @target_controller} %>

在控制器中:

def new_instant_discount
    @financefee=FinanceFee.find(params[:id])
    @target_action=params[:current_action]
    @target_controller=params[:current_controller]
    @finance_fee_category=@financefee.finance_fee_collection.fee_category
    respond_to do |format|
      format.js { render :action => 'create_instant_discount' }
    end
  end

然后我使用以下代码创建了 create_instant_discount 操作和 create_instant_dicount.rjs:

page.replace_html 'modal-box', :partial => 'instant_discount_form'
page << "Modalbox.show($('modal-box'),  {title: ''});"

并创建了我要打开的局部视图 _instant_discount_form。

这里的问题是,当我单击链接打开模态框时,没有任何反应,也没有出现错误。

你能帮我找出我错过了什么吗?

更新:Link_to_remote 问题

似乎问题出在 link_to_remote 中,因为我尝试渲染局部视图但它不起作用,我也尝试使用直接视图但它给出了相同的结果。所以我不知道为什么这个链接不起作用,请注意我将它包含在控制器中:

  include LinkPrivilege

  helper_method('link_to','link_to_remote','link_present')
4

2 回答 2

1

你能帮我找出我错过了什么吗?

如果您没有看到任何结果,则问题可能出在您的客户端 JS上。

Rails 无法为您打开模型,它必须使用服务器返回的数据附加到 DOM。看来您正在将请求发送到服务器,这只是将请求返回并附加到DOM的一种情况。


代码

当前代码非常随意,这就是我对您提供的内容所做的

#app/views/controller/action.js.erb
$modalbox = $('modal-box');
$modalbox.html("<%=j render 'instant_discount_form' %>");
$('body').append(Modalbox.show($modalbox,  {title: ''}));

这可能会也可能不会。我需要访问几个依赖项,包括您的routes控制器Modalbox代码。

调试 a) 请求是否被触发和 b) 请求是否正在处理的最佳方法是使用开发者控制台console.log输出:

#app/views/controller/action.js.erb
$modalbox = $('modal-box');
$modalbox.html("<%=j render 'instant_discount_form' %>");
$('body').append(Modalbox.show($modalbox,  {title: ''}));

console.log($modalbox); //checks if $('modal-box') is valid
console.log(Modalbox.show($modalbox,  {title: ''})); // checks if Modal being called correctly.

--

系统

您的问题不仅仅是没有收到您的数据的直接回报,您需要了解代码的结构(几乎就在那里)。

<%= link_to_remote '+ Add Discount', :url => {:controller =>  "parent_wise_fee_payments", :action => "new_instant_discount", :id =>  @financefee.id, :current_action => @target_action, :current_controller => @target_controller} %>

首先,你link_to_remote有很多依赖属性——如果你只改变了应用程序的一部分,你就必须改变其中的许多。Rails 对DRY (Don't Repeat Yourself)很重controlleraction因此不建议调用细节。

您最好使用其中一个路由助手(特别是polymorphic路径助手):

<%= link_to_remote '+ Add Discount', parent_wise_fee_payements_new_instant_discount_path(id: @financefee.id, current_action: @target_action, current_controller: @target_controller) %>

现在,从路径助手的荒谬性来看,我相信您可以理解您需要如何解决系统中的一些更深层次的问题。也就是说,您要具体说明您的控制器/操作;它需要CRUD用于数据对象:

#config/routes.rb 
resources :finance_fees do
   resources :discounts
end

#app/controllers/discounts_controller.rb
class DiscountsController < ApplicationController
   def new
      @finance_fee = FinanceFee.find params[:finance_fee_id]
      @discount = @finance_fee.discounts.new
   end
   def create
      @finance_fee = FinanceFee.find params[:finance_fee_id]
      @discount = @finance_fee.discounts.new create_params          
   end
   private
   def create_params
      params.require(:discount).permit(:x, :y, :z)
   end
end

我可以看到的最大问题是您将调用new_instant_discount作为一个动作,并带有许多其他高度调整的属性。虽然这并没有错,但它否定了 Ruby/Rails 的核心方面之一——面向对象。

您应该使用 Rails 中的对象- 即它们是在您的模型中创建的,然后可以在您的控制器等中进行操作...

你的控制器动作对我来说似乎很粗略。

--

关于您的更新,link_to_remote似乎不赞成使用remoteRailsUJS 功能:

<%= link_to "link", link_path, remote: true %>

这会将数据异步发送到您的服务器,它不会处理响应。如果您使用以下内容,它应该可以工作:

<%= link_to '+ Add Discount', :url => {:controller => "parent_wise_fee_payments", :action => "new_instant_discount", :id => @financefee.id, :current_action => @target_action, :current_controller => @target_controller}, remote: true %>
于 2015-10-01T09:55:21.970 回答
0

尝试创建 new_instant_discount.rjs。然后放在那里:

page.replace_html 'modal-box', :partial => 'instant_discount_form'
page << "Modalbox.show($('modal-box'),  {title: ''});"
于 2015-10-01T09:29:35.863 回答