Forum: Ruby on Rails access json data from rails controller in ajax call and process it

Posted by ruby rails (rubyonrails)
on 2012-12-05 12:02
I am building a Task Management System and have implemented
fullcalendar.js in it. I need to get the pending tasks and change the
color of those pending tasks. I am getting the instance variable of
pending tasks details in the rails controller. But in the ajax request I
am not able to loop it.

Please find my code below.

      $('.task_name').live('click', function () {
        alert(this.id);
        $.ajax({
            url: 'pending_task_details',
            data: {
                task_id: this.id
            },
            success: function (data, response, event, date) { <%
                for date_cell in
@pending_tasks.start_date..@pending_tasks.end_date %>
getCellFromDate(date_cell, calInstance); <% end %>
            }
        });
    });

The above is the logic I need to implement. But I am not getting
@pending_tasks.start_date in the view.

In the console I am getting the tasks details
#<Task id: 4, project_id: 4, task_name: \"Task4\", task_type: nil,
user_id: 2, description: \"Description4\", pm_id: nil, created_at:
\"2012-11-01 09:42:37\", updated_at: \"2012-11-01 09:42:37\",
percent_completed: \"12\", status: \"completed\", due_date: nil,
start_date: \"2012-11-01\", end_date: \"2012-11-08\", priority: \"low\",
days_left: nil>

In the tasks controller I am getting the pending tasks as.

      def pending_task_details
        @pending_tasks = Task.find_by_id(params[:task_id])
        p "The pending tasks are......",@pending_tasks.inspect
    #(Date.parse(@pending_tasks.start_date.to_s)..Date.parse(@pending_tasks.end_date.to_s)).each
{ |date| render :json=>[date.strftime('%a %d %b %Y')] and return}
        render :json=>@pending_tasks
      end
Posted by Jim ruther Nill (jimboker)
on 2012-12-05 14:10
(Received via mailing list)
On Wed, Dec 5, 2012 at 7:02 PM, ruby rails <lists@ruby-forum.com> wrote:

>         $.ajax({
>     });
>

you simply can't do this. try

$.ajax({
  url: 'pending_task_details',
  data: { task_id: this.id },
  dataType: 'json',
  success: function(data) {
    // do something USING javascript with data here. don't use ruby
    data.map(function(d) {
      return d.dates.each(function(date) { return
getCellFromDate(data[0].date, calInstance) });
    });
  }
})

something like this. i'm pretty sure this still has some errors but you
should be able to work with that.
just remember to add a dates key with the range of date when you render
json.


> days_left: nil>
>       end
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>


--
Posted by Dan Brooking (Guest)
on 2012-12-05 15:12
(Received via mailing list)
Hey guys beginner question here so hopefully it's an easy and quick 
answer.

Is it possible to create associations via scaffolding? Say I have an 
Artists model and a Song model and I want song to have an artist_id. Can 
I create that via the scaffold command? Or would I scaffold like normal 
for both Artist an Song and the add in the column and index for 
artist_id to Song?

Thanks
Dan
Posted by Dan Brooking (Guest)
on 2012-12-05 16:14
(Received via mailing list)
Hey guys beginner question here so hopefully it's an easy and quick 
answer.

Is it possible to create associations via scaffolding? Say I have an 
Artists model and a Song model and I want song to have an artist_id. Can 
I create that via the scaffold command? Or would I scaffold like normal 
for both Artist an Song and the add in the column and index for 
artist_id to Song?

Thanks
Dan
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.