|
|
|
|
|
by JeremyNT
1911 days ago
|
|
There are a lot of ways to skin this cat! You can also have a hidden form submission that does this from the existing controller so you don't need to specify the URL. For example, you can add a hidden submit button like form.submit 'preview', data: { composer_target: 'submit' }, hidden: true
Instead of a stimulus target, you wrap your preview area in a turbo frame <turbo-frame id="output">
...
</turbo-frame>
In your (ruby) controller you can re-use the existing controller action: def create
@post = Post.new(post_attributes)
preview && return if params[:commit] == 'preview'
...
end
Do the turbo junk in a private method for the preview: private
def preview
render turbo_stream: turbo_stream.replace(
'output', partial: "posts/preview", locals: { post: @post }
end
end
Your stimulus controller now just does this: preview() {
this.submitTarget.click();
}
I'm not sure which I prefer! |
|