Launch modal from controller
i'm trying to show a modal window (using jquery ui dialog) to the user from my controller.
For example :
def create
if params[:id]
#do something
else
#show a modal wi开发者_运维知识库ndow with a partial as content
end
Thx for your help =)
This is a fix, but probably will work for you
Controller
def create
if params[:id]
#true code
else
render :new
@dialog = true
end
end
on you new.html.erb
<% if @dialog %>
<%= #dialog code %>
<% else %>
<%= #normal code %>
<% end %>
Hope it works.
UPDATE
If what I asked you in comment is correct then you can do this
in your new.html.erb
you can do this
<% form_for @product do |f| %>
<% if @product.errors %>
<%= #dialog code %>
<% end %>
<% end %>
You can't show a jQuery dialog from a controller, because you can't execute JavaScript on server side. The closest you could come to this is sending back a JavaScript string to be evaluated on the client side, but this is very bad practice. Your best bet is to key off something on the server side and do your dialog code on the client side. If you don't want a bunch of JavaScript in your erb templates, you can call a function from there, or you can do your submit via AJAX and either have your handler do the dialog or render a js.erb template that can both render partials and do JavaScript. Let me know if you want an example of any of the above and I'll see if I can provide one.
精彩评论