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

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.

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. :)

JS is not executing rails

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.

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.

backbone-rails: redirection to a controller action

I am using rails-backbone gem for the first time. I have to doubt in how to redirect in using backbone. Following the steps that i followed as provided at github's page and everything is working fine. but when i see my controller it contains the following line in create action
respond_to do |format|
if #login.save
format.html { redirect_to #login, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #login }
which works great but I can see that for json object it renders show action and hence not possible to reload the page as it takes ID as attribute, I can use a static page for display is one solution but i need it to redirect it to show action rather than rendering it as done when the format is html. how can that be done?
This is my complete controller:
class LoginsController < ApplicationController
before_action :set_login, only: [:show]
def index
end
def new
#login = Login.new
end
def show
end
def create
#login = Login.new(login_params)
respond_to do |format|
if #login.save
format.html { redirect_to #login, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #login }
else
format.html { render action: 'new' }
format.json { render json: #login.errors, status: :unprocessable_entity }
end
end
end
private
def set_login
#login = Login.find(params[:id])
end
def login_params
params.require(:login).permit(:email, :password)
end
end
There are many ways to do it.
1)In you controller instead of rendering json object, just create create.js.erb under views/logins then put js code to redirecting the page. It should be something like this,
create.js.erb:
window.location.href = <%= users_path(#user) %>
2) You can execute this window redirection js code in the backbone callback after the successfull creation of record. Please refer this backbone affcial website to know how to success callback.

loading rails partial via ajax

I'm trying to load a partial via ajax. When I try to render the partial all I get is the render code printed to the screen.
JS
$("#filter_menu2 input:checkbox").click ->
$.get "search", (data) ->
$("#products").html("<%= escape_javascript(render(:partial=>'new_shop/shop_search_results')).html_safe %>")
return
controller
respond_to do |format|
format.html #search.html.erb
format.js
end
This is just getting printed the screen
<%= escape_javascript(render(:partial=>'new_shop/shop_search_results')).html_safe %>
You have to put your code in some search.js.erb and put respond_to :js at the top of your controller and at last return format.js {render "search.js.erb"}.
So, now search.js.erb contains this line
$("#products").html("<%= escape_javascript(render(:partial=>'new_shop/shop_search_results')).html_safe %>")

Categories

Resources