JS is not executing rails - javascript

I try to display a view in a modal but i dont know why my JS is not executing, i saw similar questions about it but it doesnt solve my issue,
My link
def create
document = Document.find(params[:document_id])
order = Order.create!(doc_sku: document.doc_sku, amount: document.price, state: 'pending')
redirect_to new_order_payment_path(order, format: :js, remote: true)
end
controller:
def new
#document = Document.last
respond_to do |format|
format.js {render layout: false, content_type: 'text/javascript'}
end
end
new.js.erb
$("#newModal .modal-content").html('<%= j render "payments/form" %>');
$("#newModal").modal();
I test it with a simple alert but nothing change
In my log
Processing by PaymentsController#new as JS
Parameters: {"remote"=>"true", "order_id"=>"35"}
I dont have warnings/errors in the network or console of my browser

Content type is wrong. Change render to just.
respond_to do |format|
format.js
end
Should work.

Related

Rails 7 ActionController::UnknownFormat in CommentsController#create respond_to

I have a comments controller that i want to load a partial "create.js.erb"
class CommentsController < ApplicationController
skip_before_action :verify_authenticity_token
def create
#comment = Comment.new(comment_params)
#comment.account_id = current_account.id
respond_to do |format|
if #comment.save
#comments = Comment.where(post_id: #comment.post_id)
format.js { render "comments/create" }
else
# unable to save
end
end
end
def comment_params
params.require(:comment).permit(:message, :post_id)
end
end
My create.js.erb partial
console.log("comment created...");
$("#post-comments").html("<%= escape_javascript(render partial: 'posts/comments', locals: { comments: #comments }) %>");
If i refresh the page the comment gets posted however when i click the submit button it send me the error ActionController::UnknownFormat in CommentsController#create
In laymans terms i want the create function to hit without having to redirect.
Seems like using Hotwire and Turbo is the way to go
respond_to do |format|
if #comment.save
#comments = Comment.where(post_id: #comment.post_id)
format.html { redirect_to posts_path }
format.turbo_stream
end
end
then you need to change create.js.erb to create.turbo_stream.erb
<%= turbo_stream.prepend "comments", #comments %>
all works as intended now
Rails 7 removes the previous way of using format.js in Rails UJS and now it's all hotwire/turbo. Kinda confusing but once you learn it no more writing javascript. :)

ParksController#create is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []

I'm creating a park with a modal. Everything seems to work fine when I'm not saving a picture in the form. When I do add a picture and try to create/save, the picture gets saved, but I get an error message saying:
ParksController#create is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []
"\nrequest.variant: #{request.variant.inspect}"
raise ActionController::UnknownFormat, message
elsif interactive_browser_request?
message = "#{self.class.name}\##{action_name} is missing a template " \
"for this request format and variant.\n\n" \
I'm overlooking something, but don't see what.
controller
class ParksController < ApplicationController
skip_before_action :verify_authenticity_token
after_action :verify_authorized
def new
#park = current_user.parks.build
respond_to do |format|
# format.html { redirect_to root_url, alert: 'Page not accessible' }
format.html
format.js
end
authorize #park
end
def create
#park = current_user.parks.create(park_params)
authorize #park
respond_to do |format|
if #park.save
format.js
format.html
# end
else
format.js{render 'new'}
end
end
end
You're not redirecting to another view after the create action. You're responding to html there, but not doing anything with it. Usually after a create, the user is redirected to the index view. I think if you passed a block to the html response and redirected, the error would resolve itself.
ie:
...
respond_to :html { redirect_to index_path }
...
Also, you have your end commented out for your if #park.save

respond_to do |format| gives "UnknownFormat in UsersController#show" in rails 5 app

In my rails 5 app I want to render partials in a div in the views/users/show.html.erb on link click.
I believe I have set things up accordingly but rails gives me this error ActionController::UnknownFormat in UsersController#show
ActionController::UnknownFormat
respond_to do |format|
format.js { render :show_kwst }
end
I'm kind of lost here, I've done similar things in rail 4 by using bootstrap tabs with out any problems.
I've also added the 'responders' gem to the gemfile
This is my views/users/show.html.erb
<%= link_to 'Kwst', user_path%>
<div id="content"></div>
This is my users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#user_posts = #user.posts
#user_kwsts = #user.kwsts
respond_to do |format|
format.js { render :show_kwst }
end
end
end
And here is the `views/users/show_kwst.js.erb``
$("#content").html("<%=escape_javascript(render :partial=>"shared/show_kwst")%>");
Am I missing something?
Try to the following order, you can follow this Rails Guide About format.js
respond_to do |format|
format.html { #user_kwsts }
format.js
end
Notice the format.js in the respond_to block; that allows the controller to respond to your Ajax request. You then have a corresponding app/views/users/show_kwst.js.erb view file that generates the actual JavaScript code that will be sent and executed on the client side.

Rails respond_to js on rails controller method

I'm trying to run a js.erb file for when a worker has been updated, but nothing happens. as if it's not running the js.erb file
I have this in my controller:
def update
#worker = Worker.find(params[:id])
respond_to do |format|
if #worker.update(worker_params)
format.js
format.html { redirect_to #worker }
else
format.html {render :edit }
end
end
end
and a corresponding file at:
/app/views/workers/update.js.erb
contents of that file is a publish helper from private_pub gem
<% publish_to "/messages/new" do %>
$("#present").load(location.href + " #present");
<% end %>
I don't get any errors or anything that I can see. can't quite figure out what I'm missing
UPDATE:
I added remote: true to the update form, now the js file is being executed as expected. but the issue now is that when I press submit, he runs the js file but stops after that, he is supoused to redirect the page back to the workers show again.

Rails - adding ajax to partials, 'undefined method `render''

I have a form partial, within a div with id "chapcomments", with a submit tag -
<%= f.submit "Post", remote: true %>
In the correct view folder, I have create.js.erb -
$('#chapcomments').html("<%=j render 'shared/chap_comment_complete' %>");
And in the controller I have the format block which includes
def create
#chap_comment = current_user.chap_comments.build(chap_comment_params)
respond_to do |format|
if #chap_comment.save
format.js
format.html {redirect_to chapter_path(#chap_comment.chapter)}
else
format.html { render :new }
format.json { render json: #chap_comment.errors, status: :unprocessable_entity }
end
end
end
...but I'm not getting ajax behaviour...
Where am I going wrong?
You've made the change so that the JS is in the right js.erb file in the view, but you need to move the remote: true part from the submit tag into your form_for declaration, otherwise the format.html response block will be rendered instead of format.js.

Categories

Resources