在Rails中添加一个模态对话框?

时间:2022-04-05 10:39:08

I want to add a Ajax link into my application. When I click on the link it should pop up a modal dialog which contains event's details.. I add this link in my helper class.

我想在我的应用程序中添加一个Ajax链接。当我点击链接时,它会弹出一个包含事件细节的模态对话框。我在helper类中添加了这个链接。

 link_to(event.name, events_path(@event), :remote => true 

Then I create js file to insert content into hidden div.

然后创建js文件,将内容插入隐藏的div中。

$('.modal').html(<%= escape_javascript(render(@event)) %>); 
$('.modal').dialog(); 

In here modal is my hidden div. But it could not pop up any modal dialog. I cannot understand what is the error and why is it not working. Plz can anybody help me to correct this?

在这里,模态是我隐藏的div,但是它不能弹出任何模态对话框。我不明白什么是错误,为什么它不能工作。谁能帮我更正一下吗?

2 个解决方案

#1


2  

Change

改变

$('.modal').html(<%= escape_javascript(render(@event)) %>);

$(' .modal ')。html(< % = escape_javascript(渲染(@event))% >);

to

$('.modal').html("<%= escape_javascript(render(@event)) %>");

$(' .modal ')。html(“< % = escape_javascript(渲染(@event))% > ");

From a JS point of view, your code will be invalid because you're not wrapping your render in quotes and it will try to evaluate your HTML.

从JS的角度来看,您的代码将是无效的,因为您没有将呈现包装在引号中,它将尝试评估您的HTML。

EDIT

编辑

If you're trying to link to this on a show click, you'll need to use show.js.erb to show your modal dialog rather than create.js.erb. create will only be called if you POST a form to /events whereas here it looks like you're trying to show just the details of the event.

如果要在show click中链接到这个,需要使用show.js。erb显示你的模态对话框而不是创建。create只有在向/events发布表单时才会被调用,而在这里,看起来您只是在尝试显示事件的细节。

Put the code above (with quotes) in the show.js.erb, make sure you have a respond.js response in your show method on the controller, and try it again.

将上面的代码(加上引号)放到show.js中。嗯,一定要有回应。控制器上的show方法中的js响应,并再次尝试。

#2


0  

Could be possible that you didnt install Jquery since the rails default is Prototype library. I would upgrade to Rails 3.1 which makes it easy to use jquery:

可能您没有安装Jquery,因为rails默认是原型库。我将升级到Rails 3.1,使jquery更容易使用:

rails new example -j jquery

rails新示例-j jquery

or install jquery: http://railscasts.com/episodes/136-jquery

或安装jquery:http://railscasts.com/episodes/136-jquery

#1


2  

Change

改变

$('.modal').html(<%= escape_javascript(render(@event)) %>);

$(' .modal ')。html(< % = escape_javascript(渲染(@event))% >);

to

$('.modal').html("<%= escape_javascript(render(@event)) %>");

$(' .modal ')。html(“< % = escape_javascript(渲染(@event))% > ");

From a JS point of view, your code will be invalid because you're not wrapping your render in quotes and it will try to evaluate your HTML.

从JS的角度来看,您的代码将是无效的,因为您没有将呈现包装在引号中,它将尝试评估您的HTML。

EDIT

编辑

If you're trying to link to this on a show click, you'll need to use show.js.erb to show your modal dialog rather than create.js.erb. create will only be called if you POST a form to /events whereas here it looks like you're trying to show just the details of the event.

如果要在show click中链接到这个,需要使用show.js。erb显示你的模态对话框而不是创建。create只有在向/events发布表单时才会被调用,而在这里,看起来您只是在尝试显示事件的细节。

Put the code above (with quotes) in the show.js.erb, make sure you have a respond.js response in your show method on the controller, and try it again.

将上面的代码(加上引号)放到show.js中。嗯,一定要有回应。控制器上的show方法中的js响应,并再次尝试。

#2


0  

Could be possible that you didnt install Jquery since the rails default is Prototype library. I would upgrade to Rails 3.1 which makes it easy to use jquery:

可能您没有安装Jquery,因为rails默认是原型库。我将升级到Rails 3.1,使jquery更容易使用:

rails new example -j jquery

rails新示例-j jquery

or install jquery: http://railscasts.com/episodes/136-jquery

或安装jquery:http://railscasts.com/episodes/136-jquery