Pregunta

I have a list of tasks. Each tast has a day:date and a name:string. In the Index I would like to show only the tasks with the day of today. I would also like to paginate the days before, with a "prev" link and a "next" link if there are tasks after today.

Very simple in my head, but, I can't figure how to do it. I'm new on ruby-on-rails, and i'm having such a hard time.

Thanks.

Here's what i have by now. But it does't work.

index

<h1>Listing tasks</h1>
<% @tasks.each do |task| %>
    <%= task.name %>
   <%= task.day %>


<% end %>


<br />

<%= link_to 'New Task', new_task_path %>
<%= link_to 'Previous', tasks_url(:date => @date.prev_day) %>
<%= if @date.past? = link_to 'Next day', tasks_url(:date => @date.next_day) %>

tasks_controller

  def index

    @date = Date.parse(params[:day]) rescue Date.today
    @tasks = Task.where( Date.today )
    @total_tasks   = Task.count
    @current_tasks = @tasks.size
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @tasks }
    end
  end
¿Fue útil?

Solución

You have some little error in this script :

<%= link_to 'Previous', tasks_url(:date => @date.prev_day) %>
<%= if @date.past? = link_to 'Next day', tasks_url(:date => @date.next_day) %>

Here, you are passing a date params to the route.

@date = Date.parse(params[:day]) rescue Date.today

Here you are using a day params instead of params[:date]

@tasks = Task.where( Date.today )

This is not the correct syntax to find task by date and you did not use the @date above but the date of today, replace it by Task.where(day: @date)

I hope this would solve most of your issues, if not please give us more details about the error you encounter.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top