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.
Related
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.
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] %> )
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'm working on a very simple RoR4 forum-like application. This is the forums_controller.rb actions for creating a new forum:
def create
#new_forum = Forum.new(forum_params)
if #new_forum.save
respond_to do |format|
format.html { redirect_to forums_path }
format.js
end
else
respond_to do |format|
format.html { render 'new' }
format.js { render partial: 'form_errors' }
end
end
end
Nothing magic there. The create js.erb looks like this:
<%= add_gritter("This is a notification just for you!") %>
<%= broadcast "/forums/new" do %>
$('#forum_form').remove();
$('#new_forum_link').show();
$('#forums_table').append('<%= j render #new_forum %>')
<% end %>
Everything is actually working fine. The broadcast block call you can see uses Faye to broadcast the javascript managing the changes in the interface.
My concern is, whatever is outside of the block in the js.erb file (i.e. that add_gritter call) is totally ignored. I'm sure it's not a gritter problem because changing that line for a simple alert('hi'); does nothing. However, if the call is moved inside the block it gets executed (with the annoying effect that every client gets the notification, alert or whatever). I am really out of ideas regarding this, and would appreciate any help.
<%= broadcast "/forums/new" do %>
is spitting the result of the execution as javascript (that HTTOK) which makes it go meh. So it should be
<% broadcast "/forums/new" do %>
Note the lack of the =.
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.