你能帮我找出我错过了什么吗?
如果您没有看到任何结果,则问题可能出在您的客户端 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)很重controller
,action
因此不建议调用细节。
您最好使用其中一个路由助手(特别是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
似乎不赞成使用remote
RailsUJS 功能:
<%= 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 %>