How to make a respond_to by AJAX in Rails 3? - javascript

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.

Related

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, js response, only a notice

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] %> )

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.

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