在我的Rails应用程序中,我有一个弹出的模式,当一个学生被点击被编辑时,它会弹出。在重定向到编辑页之前,用户必须提供编辑的基本原理和名称(此信息保存在另一个数据库中)。但是,在模态中的确认按钮上,我通过jQuery分配href值,并希望页面在单击时被重定向到那里,但是它遵循rationale_controller.rb redirect_to子句中定义的路径。有没有办法让它代替HREF属性呢?
感谢所有的帮助,谢谢!
编辑:
模态按钮:<button class="btn btn-warning" id="editStudentConfirmBtn">Edit</button>
联合来分配HREF:
var kid_id = $(this).data('id');
$("#editStudentConfirmBtn").attr("href", "/kids/" + kid_id + "/edit");默认rational_controller.rb设置:
def create
@rationale = Rationale.new(rationale_params)
@rationale.user = current_user
respond_to do |format|
if @rationale.save
format.html { redirect_to @rationale, notice: 'Rationale was successfully created.' }
format.json { render :show, status: :created, location: @rationale }
else
format.html { render :new }
format.json { render json: @rationale.errors, status: :unprocessable_entity }
end
end结束
编辑2:
模式
发布于 2016-08-11 12:43:48
该解决方案需要一个AJAX调用,在执行create操作后将页面重定向到指定的位置。
我必须按照以下方式更改Rationale的控制器:
def create
@rationale = Rationale.new(rationale_params)
@rationale.user = current_user
respond_to do |format|
if @rationale.save
format.html { render :nothing => true }
format.json { render :show, status: :created, location: @rationale }
else
format.html { render :new }
format.json { render json: @rationale.errors, status: :unprocessable_entity }
end
end
end并按如下方式更改JS,而不是使用jQuery将href分配给按钮,而是使用AJAX调用自动重定向页面:
$('#editStudentConfirmBtn').click(function(){
$.ajax({
success: function(result) {
location.href = '/kids/' + kid_id + '/edit'
}
});
});https://stackoverflow.com/questions/38860087
复制相似问题