I'm Implementing Ajax with Rails by adding a new post in runtime without refreshing the page.
The problem is I want to render a specific view by this script which is allocated in create.js.erb
$('<%=render(:file => "posts/post.html.erb)%>').insertBefore($('.posts').children('div.post-box').first());
But it doesn't work, Can anyone tell me what's wrong on this script and how to select html code in jquery from a specific view?
and this's the create action
def create
#post = Post.new(params[:post])
respond_to do |format|
if #post.save
format.html { redirect_to :back, notice: 'Post was successfully created.' }
format.js
end
end
All you need is add escape_javascript method, just like this
$('<%= escape_javascript(render(:file => "posts/post.html.erb"))%>').insertBefore($('.posts').children('div.post-box').first());
Related
I'm running Rails 2.3.8 and I have a respond_to in my Projects controller create action.
def create
respond_to do |format|
format.html { redirect_to('/') }
format.json :status => 200
end
end
I have an AJAX call to this action. The Rails application then renders
projectdocs/create.erb
My question is, how can I change this file path within my action from create.erb to create.erb.js.
I think your controller-name would be "projectdocs" an not "Project" if he renders "projectdocs/create.erb", but that's not the point.
You can explicit render a js-file using
format.js { render :action => 'create' }
if this Format is requested.
It depends on called format. If client wants js, add format.js and rails will try to render first of all create.js.erb
I need to pass two instance variables to a javascript file that is being used by an ajax request to update a user display. This is what I need to do:
respond_to do |format|
if #post.save
format.js { #post #user_vote } # <-- right here
else
format.html { redirect_to :back, :alert => 'There was an error in removing the vote' }
end
end
How is this done?
There is no need to pass the instance variables if you use js.erb files. You can directly put the rails tag and access those variables inside the js.erb file
Eg:
In your controller just put
format.js #instead of format.js { #post #user_vote }
and in js.erb file you can access the instance variable as
$('#ele').html("<%= #post.name %>");
The instance variables in your ActionController action are available in your views automagically. E.g. your controller:
# posts_controller.rb
def update
# Your implementation here
#post = ...
#user_vote = ...
respond_to do |format|
if #post.save
format.js
format.html { redirect_to post_path(#post) }
else
format.js { ... }
format.html { redirect_to :back, ... }
end
end
end
Then in your update.js.erb:
# update.js.erb
console.log('Post: <%= #post.inspect %>');
console.log('User vote: <%= #user_vote %>');
# Your JS implementation here
(Also I noticed your logic in the respond_to block is probably going to cause problems. You should render both js and html formats for both success and failure conditions of #post.save.)
I've tried different variants of make flash[:notice] working without reload.
Stackoverflow gave me this - How do you handle Rail's flash with Ajax requests?, but I can't find solution, that worked for me.
For example, added to my controller:
def create
#entry = Entry.new(params[:entry])
respond_to do |format|
if #entry.save
format.html { redirect_to #entry, notice: 'Entry was successfully created.' }
format.js {
flash.now[:notice] = 'Website was successfully created.'
render action: 'create'
}
else
format.html { render action: "new" }
format.js { render action: "new" }
end
end
end
create.js
$('<%= j render #website %>').appendTo('#websites').hide().fadeIn();
$(".alert").html("<%= escape_javascript(flash[:notice]) %>"); $(".alert").show(300);
$("#new_website")[0].reset();
but it didn't work.
Can someone tell me understandable full solution, that worked for him ?
Do you make a typo on your js template filename? it should be create.js.erb but not create.js
and please strictly follow the https://stackoverflow.com/a/8873592/557863 , then make your changes on it.
You're submitting new entries via Ajax, so you shouldn't use the flash -- it's intended for redirect responses, but you're not redirecting. To indicate success or failure, you'd need to write that into your JavaScript response.
railsnoobquestion: I am trying to develop a feature where a user can save an object in rails and is then forwarded to the form again, to create another object.
I could think of two options:
-Create a fully new route
-Add data to the restaurant post object, check for that data in the controller?
Has anyone built a similar feature?
thx
Here's a standard create action for the posts resource.
def create
#post = Post.new params[:post]
if #post.save
redirect_to #post, notice: 'Post was successfully created.'
else
render :new
end
end
All you would need to do is change the redirect from #post to new_post_path.
redirect_to new_post_path, notice: 'Post was successfully created.'
Is that what you're looking for?
The solution was to create a hidden form field with another submit button:
%input#create_another{:name => "create_another", :type => "hidden", :value => 0 }
%a.btn.btn-info#submit_another
Then use javascript to submit the form:
jQuery(document).ready(function() {
$("#submit_another").click(function() {
$('#create_another').attr('value','1');
console.log($('#create_another').attr('value'));
$(".formtastic").submit();
return true;
});
});
Inside the respective controller, in my case, category controller:
if params[:create_another].nil?
#create_another = false
else
#create_another = (params[:create_another] == "1")
end
respond_to do |format|
if #category.save
if #create_another
format.html { redirect_to new_restaurant_category_path(#restaurant), notice: I18n.t(:entry_successfully_created, :name => I18n.t(:category_singular)) }
I would like to do a conditional update in ruby on rails 3.1
Where based on the location you came from, after update, an redirect will be done.
Splitted my 1 big form in to separate smaller ones, so now the Idea is to redirect to the correct subform.
For example the form can be submitted from:
profile basics form
Profile details form
The only thing I could come up with is checking the action name and use that to redirect. But its very ugly and long code and not fully working either. What would be the railsway of doing this?
This is my controller update action:
def update
#profile = Profile.find(params[:id])
respond_to do |format|
if #profile.update_attributes(params[:profile])
format.html { redirect_to #profile, notice: 'Profile was successfully updated.' }
else
format.html {
render :action => "edit_basics"
#
}
end
end
end
Why not just pass the redirect location as a hidden_field in the form, then have each form set it as needed:
redirect_to params[:redirect_location]
You could also do this using steps or something if you don't want to expose the raw string in your HTML:
redirect_to location_for_step(params[:step])