hello guys, I''m trying to make a ajax request and return some data from
my controller, but how I do it to call the JSON in my JS after the
request ?
controller task
def create
@task = Task.create( :project_id => params[ :project_id ], :name =>
params[ :task ] )
if @task.valid?
@return = { :task => @task.name, :project_id => @task.project_id }
else
@return = { :error => "Ocorreu um erro inesperado. Tente
novamente." }
end
return ActiveSupport::JSON.encode( @return )
end
view index - JS
$( "#tasks_form" ).submit( function( e ) {
e.preventDefault();
var task = $( this).find( "textarea" ).val();
$.post( "<%= url_for :controller => :task, :action =>
:create
%>", { task: task, project_id: <%= @project_id %> }, function( e )
{
}, "json");
} );
Thank you
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Check here: http://api.jquery.com/jQuery.post/ There''s an example titled "Example: Posts to the test.php page and gets contents which has been returned in json format" about half way down the page. On Thursday, June 14, 2012 5:45:07 PM UTC-5, Ruby-Forum.com User wrote:> > hello guys, I''m trying to make a ajax request and return some data from > my controller, but how I do it to call the JSON in my JS after the > request ? > > controller task > > def create > > @task = Task.create( :project_id => params[ :project_id ], :name => > params[ :task ] ) > > if @task.valid? > @return = { :task => @task.name, :project_id => @task.project_id } > else > @return = { :error => "Ocorreu um erro inesperado. Tente > novamente." } > end > > return ActiveSupport::JSON.encode( @return ) > > end > > view index - JS > > $( "#tasks_form" ).submit( function( e ) { > > e.preventDefault(); > var task = $( this).find( "textarea" ).val(); > > $.post( "<%= url_for :controller => :task, :action => :create > %>", { task: task, project_id: <%= @project_id %> }, function( e ) { > > }, "json"); > > } ); > > Thank you > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Z-OA1t0d7ToJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Try using basic jquery ajax call i.e.
$.ajax({
url : "your_url"
// JSON data
}).done(function(response) {
// here response will have JSON returned by task#create
});
пятница, 15 июня 2012 г., 1:45:07 UTC+3 пользователь Ruby-Forum.com User
написал:>
> hello guys, I''m trying to make a ajax request and return some data
from
> my controller, but how I do it to call the JSON in my JS after the
> request ?
>
> controller task
>
> def create
>
> @task = Task.create( :project_id => params[ :project_id ], :name
=>
> params[ :task ] )
>
> if @task.valid?
> @return = { :task => @task.name, :project_id =>
@task.project_id }
> else
> @return = { :error => "Ocorreu um erro inesperado. Tente
> novamente." }
> end
>
> return ActiveSupport::JSON.encode( @return )
>
> end
>
> view index - JS
>
> $( "#tasks_form" ).submit( function( e ) {
>
> e.preventDefault();
> var task = $( this).find( "textarea" ).val();
>
> $.post( "<%= url_for :controller => :task, :action
=> :create
> %>", { task: task, project_id: <%= @project_id %> },
function( e ) {
>
> }, "json");
>
> } );
>
> Thank you
>
> --
> Posted via http://www.ruby-forum.com/.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/9k4IJ7xJkW4J.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you guys for the answer, but I just did this: render :json => ActiveSupport::JSON.encode( @return ) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You can read more about rendering JSON here: http://guides.rubyonrails.org/action_controller_overview.html#rendering-xml-and-json-data Den söndagen den 17:e juni 2012 kl. 20:17:32 UTC+2 skrev Ruby-Forum.com User:> > Thank you guys for the answer, but I just did this: > > render :json => ActiveSupport::JSON.encode( @return ) > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8oyO_5CavwMJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You can also use respond_with method and change your render :json => ActiveSupport::JSON.encode( @return ) to respond_with @return But for using this, you have to specify responds_to :json in controller воскресенье, 17 июня 2012 г., 21:17:32 UTC+3 пользователь Ruby-Forum.com User написал:> > Thank you guys for the answer, but I just did this: > > render :json => ActiveSupport::JSON.encode( @return ) > > -- > Posted via http://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/5K0hPK4pMfQJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.