In my rails app I would like to prevent users from deleting a post given some conditions. When that happens, I would like to just inform them that the post could not be deleted. The code looks like this:
def destroy
respond_to do |format|
if some_conditions
flash[:notice] = "Could not be deleted"
...
format.js {What to put here}
else
...
end
end
end
What should I put in the js response brackets so that the page is not reloaded but the notice is displayed? Thank you!
You can put the flash inside like so format.js { flash[:notice] = "Could not be deleted" } and it would compile the same. To print the notice: create a destroy.js.erb as Sravan pointed out.
create a div with a class="errors"
append the flash[:notice] by:
$('.errors').append("<%= flash[:notice] %>")
Just first, leave the format.js as it is, we will work this from the js file.
def destroy
respond_to do |format|
if some_conditions
flash[:notice] = "Could not be deleted"
format.js
else
end
end
end
Now create a file named destroy.js.erb in app/views/your controller name
in that file, destroy.js.erb
alert("Could not be deleted")
or if you want to display your notice,
take a div in your view file,
<div class="something"></div>
in your js.erb,
$('.something').text(<%= flash[:notice] %> )
Related
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
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.
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.
I'm trying to understand AJAX requests in Rails. I have a form that I currently submit using remote: true. I want to respond with an HTML redirect if the request is successful, and run an error message with Javascript if it is unsuccessful. However, no matter what the outcome is, the request seems to expect a .html as the return.
respond_to do |format|
if conversation
format.html { redirect_to(conversation_path(conversation)) }
else
format.js
end
end
This is called after I save the conversation call on AJAX. On a successful save, the path HTML is correctly sent back, but is not rendered on the client. But on an unsuccessful save, it expects the .html and throws an error. How do I accept .js as a response? My goal is to just pop up an error if the call is unsuccessful and redirect_to on a successful call.
Edit: My form_for:
<%= form_for :conversation, url: :conversations, remote: true, html: { class: "conversation-form" } do |f| %>
Here's a suggested alternative to your end-goal - in the controller, drop the format.html entry in your respond_to block. Also, set conversation to an instance variable that the view template can access:
class YourController < ActionController::Base
def your_action
# awesome code doing stuff with a conversation object goes here
#conversation = conversation
respond_to do |format|
format.js # your_action.js.erb
end
end
end
Then, put the redirect logic in your javascript view template (for the above example: app/views/.../your_action.js.erb):
<% if #conversation.errors.any? # or whatever condition you want to show a popup for %>
// put your javascript popup code here
alert('Errors happened!');
<% else %>
// this is how you do a redirect using javascript:
window.location.href = "<%= conversation_path( #conversation ) %>";
<% end %>
Hope this helps!
I have a form, that I send with using AJAX. The form is in the FirstsController. I need the form send to SecondsController. The form is sent and data saved. Then I wanna change the form by a text, I try to do by this way:
def create
...saving...
respond_to do |format|
format.js {
render :update do |page|; page << "$('#id_element').html('hellllloooooo');" end
}
format.html {}
end
end
And I ma getting this error:
ActionView::MissingTemplate (Missing template seconds/update, application/update with {:handlers=>[:erb, :builder, :coffee], :formats=>[:js, :html], :locale=>[:en, :en]}.):
How should I set the JS request back to the FirstsController? + is needed any help *.js file?
This is the right set up to do it:
def create
...saving...
respond_to do |format|
format.js {
render 'firsts/action_that_you_want'
}
format.html {}
end
end
And in the FirstsController is needed to have created the the file action_that_you_want.js.erb.