I know this isn’t valid code, but is there a way to do something like this in Rails?:
render "$('#dialog').replaceWith(#{render :action => 'new.html.erb'});"
What I’m trying to do basically is replace the contents of a JS dialog with what is/would be returned from calling render 'new.html.erb'.
Edit for @Devin M:
controller action:
def new
@act = Act.new(:user_id => current_user.id)
end
def create
@act = Act.new(params[:act])
if @act.valid?
@act.save
else
render :action => :new
end
end
new.js.erb:
$('#dialog').replaceWith("<%= escape_javascript(render(:action => 'new.html.erb')) %>");
Full error:
Showing app/views/acts/new.js.erb where line #1 raised: undefined method `formats' for nil:NilClass
You should split this code out into a seperate view since including it in the controller would go against the ideas of MVC. I would update the controller to respond to JS requests using some code like this in the action I wanted to modify:
And create a view like this with the extenstion
.js.erb:You can then trigger this JS with a remote link to the action or by adding your own UJS.