backbone-rails: redirection to a controller action - javascript

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.

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

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.

Redirecting and rendering a notice with format.js

I have come across an a scenario where I'm not sure if things could be handled better.
When using remotipart to upload files through an ajax call, all actions are carried out by js
Processing by DocumentsController#create as JS
I couldn't get the page to redirect using a js response, but then I found a solution on here
if #document.save
format.html { redirect_to root_path, notice: success_save_msg }
format.js { render :js => "window.location.href='"+root_path+"'" }
else
format.html
format.js { render action: 'create.js.erb' }
end
So the page does redirect but i no longer get a notice like I do if I was rendering html.
Is there a better way to handle this or a way to show the notice with my current implementation
Update
I have done this so far (in essence the same thing I know, but thought a little cleaner as redirect path not hard-coded in the controller)
if #document.save
format.html { redirect_to root_path, notice: success_save_msg }
format.js { render action: 'redirect.js.erb }
else
format.html
format.js { render action: 'create.js.erb' }
end
redirect.js.erb
window.location.replace("<%= root_path %>");
Though this still doesn't feel right.
Update 2
I have pieced this together, though please feel free to add cleaner solutions, I would really like to know how to handle this in the most efficient and correct way.
def create
#document = current_user.documents.new(document_params)
respond_to do |format|
if #document.save
format.html { redirect_to root_path, notice: success_save_msg }
format.js { flash[:notice] = 'File Successfully Uploaded'
render action: 'redirect.js.erb'
}
else
format.html
format.js { render action: 'create.js.erb' }
end
end

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.

Render javascript for partial - Rails 3

I'm still relatively new to rails and getting used to a few things. Just a quick question really, I have an AJAX form (with :remote => true etc...) which works and sends the data to the server without a pageload.
My question is around the javascript file that can be used after the post. My partial is called '_newsomething.html.erb' and once it's posted I want to execute a javascript file - do these files have to be named after actions in the controller to sync and work properly in rails? At the moment I have a javascript file called 'create.js.erb' with just an javascript alert in it, but it's not firing.
e.g. do I need to have a partial called '_create.html.erb' and then a javascript file called 'create.js.erb'? Is there anyway of telling Rails where to go for a javascript file from within the controller?
Thanks in advance!
EDIT: Controller code below (very basic and generated at the moment)
def create
#snip = Snip.new(params[:snip])
respond_to do |format|
if #snip.save
format.html { redirect_to(#snip, :notice => 'Snip was successfully created.') }
format.xml { render :xml => #snip, :status => :created, :location => #snip }
else
format.html { render :action => "new" }
format.xml { render :xml => #snip.errors, :status => :unprocessable_entity }
end
end
end
In your controller action you have to have:
respond_to do |format|
format.html { .... }
format.js
end
The format.js is the important part. If Rails discovers that you are POSTing through AJAX, then it will render _create.js.erb partial.

Categories

Resources